Issue with loading user class - php

I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:
Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36
Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36
in the game_header.php file I have
$user = new User($_SESSION['id']);
And thought it would work, but sadly it does not.
I have also tried
$user = new User($_SESSION['id']);
on the edit_account.php page, but I was getting the same error.
Here's the edit_account.php code.
Does anyone know what I might be doing wrong here?
include "game_header.php";
$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';
switch($_GET['type']) {
case 'profileoptions' : profile_options(); break;
default : profile_options();
}
function profile_options() {
echo '
';
include 'game_footer.php';
}

When encased into a function you must do the following:
global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.
So it must start with:
function profile_options() {
global $user ;
//The rest of code
}
However, I recommend you to create a parameter:
function profile_options(User $user){
//Much code
}
Then call it where $user is accessible:
profile_options($user) ;

Related

URL is not working when it's trying it for routing

I'm trying to catch the URL from my localhost, here it is
http://localhost/mvc/index.php?url=Index/category and things are going well but when I'm trying to use the URL /category it is showing error. Here is the error
Notice: Array to string conversion in C:\xampp\htdocs\mvc\index.php on line 21
Notice: Undefined property: Index::$Array in C:\xampp\htdocs\mvc\index.php on line 21
Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\mvc\index.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mvc\index.php on line 21
<?php
include_once "system/libs/main.php";
include_once "system/libs/Dcontroller.php";
include_once "system/libs/Load.php";
?>
<?php
$url = isset($_GET['url']) ? $_GET['url'] : NULL;
if ($url != NULL) {
$url = rtrim($url,'/');
$url = explode("/", filter_var($url,FILTER_SANITIZE_URL));
} else {
unset($url);
}
if (isset($url[0])){
include 'app/controllers/'.$url[0].'.php';
$ctlr = new $url[0]();
if (isset($url[2])) {
$ctlr->$url[1]($url[2]);
} else {
if (isset($url[1])) {
$ctlr->$url[1](); //Here is the line where I'm getting the
error
} else {
}
}
}else{
include 'app/controllers/Index.php';
$ctlr = new Index();
$ctlr->home();
}
?>
But when I'm using
category() instead of $url[1]
it's working fine. Here is the Index class.
<?php
class Index extends Dcontroller
{
public function __construct()
{
parent::__construct();
}
public function home()
{
$this->load->view("home");
}
public function category()
{
$data = array();
$catModel = $this->load->model("CatModel");
$data['cat'] = $catModel->catList();
$this->load->view("category", $data);
}
}
two things straight away: "/" is not legal in a url param as part of a get string. you need to encapsulate it with URL encodeing
EG:
http://localhost/mvc/index.php?url=Index%2Fcategory
it's also the fact that "$ctlr->$url[1]" simply doesn't have a function called it.. eg: whatever "$ctlr->$url[1]" resolves to ??category()?? doesn't exist, you need to MAKE it.
add this to your code
function category() {
Index tmp = new Index();
tmp->category();
}
EDIT : I've just noticed, it's even more idiotic than I thought.. your string says Index/category doesn't it?.. make the class method static.. (this code is dreadful by the way it displays almost no sound knowledge of design whatsoever) there is no Index/category because you can't call category inside a class except if it's a static method.
Learn to code.

PHP defining undefined classes in MVC directories

So I have four classes in a model directory but my index.php file for a web application can not access the classes. The IDE keeps telling me my that my classes are undefined and when I run the web-app I get these errors:
Notice: Undefined variable: CategoryDB in C:\xampp\htdocs\ex_starts\ch14_ex1\product_manager\index.php on line 28
Fatal error: Call to a member function getCategory() on a non-object in C:\xampp\htdocs\ex_starts\ch14_ex1\product_manager\index.php on line 28
I thought using require statements would include all the classes and their functions but I seem to have made a mistake. Could someone point out where I went wrong?
Here is the index.php require statements:
<?php
require('../model/database.php');
require('../model/category.php');
require('../model/category_db.php');
require('../model/product.php');
require('../model/product_db.php');
$CategoryDB = new CategoryDB();
$ProductDB = new ProductDB();
$action = (if and else statements with class method calls below) {
if ($action == 'list_products') {
$category_id = filter_input(INPUT_GET, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE) {
$category_id = 1;
}
// Get data (the CategoryDB and ProductDB have an undefined class warning here)
$current_category = $CategoryDB->getCategory($category_id);
$categories = $CategoryDB->getCategories();
$products = $ProductDB->getProductsByCategory($category_id);
else if{
//code here
}
}
?>
Class names are correct but every function call gives the same class undefined warning in my IDE. Any help in understanding what the correct way to do this would help and be appreciated, Thanks!

Illegal string offset Warning PHP '#type' in VBridgeApp->create()

I get a strange PHP error after updating my php version to 5.4
This is my function
protected function create() {
//if (VBRIDGE_DEBUG)
//drupal_set_message(__CLASS__ .'::'.__METHOD__);
$path = $this->vbridge_root_path;
$path_vbridge = $path . '/' . VBridge::VBRIDGE_CLASS_PREFIX;
$subclass = $this->getClass();
foreach ($this->_objclass as $objclass) {
if (!$this->createObj($path, $objclass, $subclass)) {
$this->createObj($path_vbridge, $objclass);
}
}
if (self::getStatus()) {
return false;
}
// Set User Session Qookie
//$this->getUser()->setQookie($this->getQookie());
// Set User Session
$this->getUser()->setSession($this->getSession());
$this->getSession()->setQookie($this->getQookie());
//$this->getUser()->setAuth($this->getAuth());
// Set User Pass
$this->getUser()->setPass($this->getPass());
// Set Auth
$this->setAuthMethods();
$this->setAuthStorages();
//
foreach ($this->getConfig() as $config) {
if ($config['#type'] == '#class') {
//createObj($config['#name'], $config['#type'], $config['#class'], $config['#path'], $appData['#config']);
}
}
return true;
}
This is the line that gives
if ($config['#type'] == '#class') {
I've looked at similar questions but haven't figured out how to fix this. Any assistance would be helpful.
Edit: Yes, I did put wrong code up last night. I was very tired after trying to tangle with this.
The error message you've posted doesn't match the line of code you say it originates from. You should get a different error for that, specifically to do with only using variables by reference.
Your code should be
$accounts = user_load_multiple(array(), array('name' => $login));
$account = array_shift($account);
But Drupal already has a helper method for that, so you might as well use it:
$account = user_load_by_name($login);

Calling model from codeigniter library shows error

Hello I have created a library Auth.php in application/libraries to authenticate if the current user is logged in or not. So my controller is :
$session_data = $this->session->userdata('logged_in');
$this->load->library('Auth');
$auth = new Auth;
$user_id = $auth->authenticate($session_data);
And the library :
class Auth{
function authenticate($vars){
$CI =&get_instance();
$CI->load->model('adminmodels/login_model');
$username = $vars['username'];
$password = $vars['password'];
$user_id = $vars['user_id'];;
$user_type = $vars['user_type'];
$check_login = $this->login_model->login($username,$password); //Line 14
if($check_login){
$user_id = $user_id;
}else{
$user_id = 0;
}
return $user_id;
}
}
But it is showing error like :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$login_model
Filename: libraries/Auth.php
Line Number: 14
Whats wrong I am doing ??
While correct generally, in CI you should call the library like this, not with the new keyword:
$this->load->library('Auth');
$user_id = $this->auth->authenticate($session_data);
Also, since you assigned the global CI object to a variable, you can't use $this to refer to it:
$check_login = $this->login_model->login($username,$password); //Line 14
should be:
$check_login = $CI->login_model->login($username,$password); //Line 14
(since you loaded the model there: $CI->load->model('adminmodels/login_model'); )
Inside Codeigniter library file, you cannot load any model, config, or any external library using $this by pass without CI instance object/variable
For your case in Auth.php at line 14, you are wrong :
$check_login = $this->login_model->login($username,$password); //Line 14
It should be like this (if access from internal function only) :
$check_login = $CI->login_model->login($username,$password); //Line 14
Or, if access from any functions in the Auth class :
$check_login = $this->CI->login_model->login($username,$password); //Line 14
But first, you must make CI intance object in your construct function like this :
$this->CI =&get_instance();
$this->CI->load->model('adminmodels/login_model');
before you action in second alternative above.
May help you...
Thanks

Call a member function on a non object

i want to display informations about object Activity that the function getCurrent() from the ListActivity should returns.
When i try it, it works perfectly, i have the information needed from the class, but, i have this error message on the top of the page :
Fatal error: Call to a member function getIdentifiant() on a
non-object in
/Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php
on line 34
Line 34 is here :
while($listActivities->next())
{
$current = new Activity();
$current = $listActivities->getCurrent();
echo $current->getId(); // line 34
}
And this is the getCurrent() function which return an Activity object.
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
}
I don't understand why i have this problem since it returns me the object that i want.
Please help me figuring it out. Thanks.
echo $current->getId(); // line 34
Fatal error: Call to a member function getIdentifiant() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php on line 34
WhatEVER you think happens, or whatEVER you see in your page, if the error says $current is not an object, it is not. It might not be null, but it could also be an array or anything that is not an object.
Also :
$current = new Activity();
$current = $listActivities->getCurrent();
Doesn't really makes sense to me.
Use
var_dump($listActivities->getCurrent());
To see what it exactly returns, and trust what errors say.
EDIT : And you may not even be looking at the right php script acually : The error says "getIdentifiant" while the code says "getId". Make sure you're looking at the right piece of code and refreshing the right page.
first set $this->current after $current = new Activity(); (maybe in constercture)
and you should return false if !isset($this->activities[$this->current])
also if you use $current = $listActivities->getCurrent() you lost your Activity object , it should save into another variable
here new code :
while($listActivities->next())
{
$current = new Activity();
if( $listActivities->getCurrent() )
echo $current->getId(); // line 34
}
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
return false;
}

Categories