Re: Sessions and Objects using PHP4
| From: | Jochem Maas | Date: | Sat, 15 Jan 2005 15:00:09 +0000 |
| Subject: | Re: Sessions and Objects using PHP4 | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-206431@lists.php.net to get a copy of this message | ||
David OBrien wrote:
I have RTFM and TTFE and still am having a heck of a time getting my objects to play well between pages The only real examples are in the notes for classes & objects http://us2.php.net/manual/en/language.oop.php(are you on php4 or php5 btw?)
Basic usage <? include_once("config.inc"); include_once("class.php");indeed, you must include classes for objects stored in a session before starting the session. :-)
$songbook = new songbook(); // defined in the class.phpif you are creating this object on every page/call and then assigning it to the session it will always have a blank/null userid. because each call overwrites the old object thats stores with a brand new one. so you need to check the session to see if a songbook object is already available. so you only ever see the login screen in your example.
session_register('songbook');alternative is using $_SESSION. but if you do you must call session_start() first: session_start(); if (!isset($_SESSION['songbook']) || !is_object($_SESSION['songbook'])
|| !(get_class($_SESSION['songbook']) == 'songbook')){ $_SESSION['songbook'] = new songbook; }
$songbook->get_stats(); $songbook->populate_manu(); $songbook->populate_series(); if (!isset($songbook->userid) || $songbook->userid == "") {header("Location: login.php");} Now the login.php is basically the same but if the login correctly it should set $songbook->userid to the username require('common.php'); session_start(); include_once("class.php"); $songbook = new songbook(); session_register('songbook'); include("newheader.php"); if ( isset($_POST['submitit'])) {$link = dbconnector(); $sql = "select * from userinfo where lower(username)=lower('".$_POST['user']."') and lower(password)=lower('".$_POST['pass']."')"; $result = mysql_query($sql); if( mysql_num_rows($result) > 0 ) { $songbook->userid = strtolower($_POST['user']); header("Location: green.php"); } else { $errormsg = "<font size=3>Invalid Username or Password.<br>\nPlease Try Again.</font>"; }} So I guess what I am asking is how DO you make objects work across pages? -Dave