Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP session object

Featured Replies

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

// 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();
                      }

  • Author

really dont understand what you mean by that code snippet :S

  • 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?

  • 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

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 by maslan

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.