October 2, 201115 yr Hi, just building a silly little app in php and i have come across a problem. When a user logs in I store a person object in the session like so if($row = mysql_fetch_array($result)){ session_start(); $person = new person(); $person->set_id($row['ID']); $person->set_name($row['Name']); $person->set_email($row['Email']); $person->set_password($row['Password']); $person->set_isAdmin($row['isAdmin']); $_SESSION['person'] = $person; $GLOBALS['main'] = "Login Sucessful"; } else{ $GLOBALS['main'] = "Login failed"; } However when i try and accsess the data in the session like this if($_SESSION['person']==null){ echo $_SESSION['person']->get_id(); } I get the following error: Fatal error : Call to a member function get_id() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/Fifa/functions/table.php on line 16 Anyone know whats going on?? thanks
October 2, 201115 yr // if variable is NULL if($_SESSION['person']==null){ // try and use NULL as an object echo $_SESSION['person']->get_id(); } if($_SESSION['person'] instanceof 'person'){ echo $_SESSION['person']->get_id(); }
October 2, 201115 yr Author It seems to work fine when I accsess the object on the page its created on however if i try and use it on the index page or any other page i get the error listed above :S?
October 4, 201115 yr Author I found the problem. I was trying to call the session on a page called table.php which was using AJAX to add itself to the index page so I could keep the table data up to date all the time. However it appears that the session data is empty if I use Ajax to add table.php to the index page as using the stranded include_once('functions/table.php'); allows me to access the session data. Does anyone know how to get sessions and AJAX working nicely?? Thanks Ben Gibson
October 8, 201115 yr Java is working on client side so won't be able to read session which is on server side. However, if you create java during creating page, you may use session object Please check the code below echo '<script type="text/javascript">' .' function yourFunciton()' .' {' .'var ses_value="'.$_SESSION['whatever'].'"'. .'//Your code here to use SESSION Object' .' }' .'</script>'; Edited October 8, 201115 yr by maslan
Create an account or sign in to comment