lilywong Posted July 25, 2006 Share Posted July 25, 2006 I have define the session in login.php, and index.php can retrieve it from login.php, however when i click a hyperlink to page1.php, i cannot get the session. i have check through but i can't find the error, below is my source code. thanks.[b]login.php[/b]$this_user = new Obj_UserRole($UserID); // UserID is taken from login username// register sessions is in this line/$user = $this_user;[b]index.php[/b]session_start();$user = $_SESSION["user"];if (session_is_registered("user")) { echo "is register"; echo $user->user_role; //this can print out successfully !}else{ session_register("user"); }<a href="page1.php">CLICK TO PAGE 1</a>[b]page1.php[/b]session_start();if (session_is_registered("user")) { echo "is register"; echo $user->user_role; //this CANNOT print out successfully ! I wonder why ???????}else{ session_register("user"); } Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/ Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 [quote]echo $user->user_role; //this CANNOT print out successfully ! I wonder why[/quote]Because... whatever $user is, it is [b]not[/b] a session variable. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63200 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 but how come i can print out the dispaly in index.php ????so how should i define the session in login.php or index.php, so that other pages also can get the session variable ? thanks thanks Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63202 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 Because of this...[code=php:0]$user = $_SESSION["user"];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63204 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 Also... be aware that the use of [url=http://php.net/session_register]session_register[/url]() has long been depricated. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63205 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=thorpe link=topic=101754.msg402965#msg402965 date=1153791849]Because of this...[code=php:0]$user = $_SESSION["user"];[/code][/quote]what's the probelm with this line ? i use this to retrive the session, should i disable it ? Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63206 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 login.php$this_user = new Obj_UserRole($UserID); // UserID is taken from login username// register sessions is in this line/$user = $this_user;===============================function Obj_UserRole($ID){ $this->userid=$ID; // some database query here...... // so can retrive the db result, become $this->user_role = $db->field('Role');}===========================so when i echo something in login.php,$user = $_SESSION["user"];echo $user->user_role; // it works. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63209 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 [quote]what's the probelm with this line ? i use this to retrive the session, should i disable it ?[/quote]There is nothing wrong with that line! You actually need to add that line to [b]page1.php[/b] if you expect[code=php:0]echo $user->user_role;[/code]to work. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63211 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 well i added the line echo $user->user_role; in page1.php, but it comes out blank page, it print nothing. :( Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63214 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 Post your actual (relevent) code for each page. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63219 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 it seems like, if i include(page1.php), when i echo $user->user_role, i can see the display.but if i use hyperlink, the session is gone. :( Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63222 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 Read my previous post. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63224 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 <b>index.php</b><?require ("config.php");session_start();$cur_user = $_SESSION["cur_user"];if($action == 'logon'){ //process login //goes to this page if login success include(login.php);}else { //goes to this page if login failed include('verify.php');}if (session_is_registered("cur_user")) { echo $cur_user->cur_role; echo ""; echo $cur_user->cur_userid; //all this cur_role and cur_userid can be displayed}else { //echo '<--- is not registered ---->'; session_register("cur_user");}echo "<a href='page1.php'>page 1[/url]";?><b>pro_login.php</b><?if (!session_is_registered("cur_user")) session_register("cur_user");$UserID = $_POST["UserID"];$Password = $_POST["Password"]; if(isset($UserID)){ $this_user = new Obj_Role($UserID); //link to role.php, to get record $this_pass = $this_user->getPasswd(); $this_status = $this_user->getStatus(); /* register sessions */ $cur_user = $this_user; } }?><b>role.php</b><?class Obj_Role{ // constructor method function Obj_Role($ID){ $this->cur_userid=$ID; $db = new DB_Re; $tbl_name = 'User'; $query = "SELECT * FROM $tbl_name WHERE ID = '$this->cur_userid'"; $db->query($query); if($db->next_record()){ $this->cur_passwd = $db->f('Passwd'); $this->cur_role = $db->f('Role'); $this->cur_domain = $db->f('Name'); } $db->free();}?><b>verify.php</b><?if (!session_is_registered("cur_user") || !is_Object($cur_user)){ echo "login failed!"; session_destroy(); include($Base_Temp_Dir.'logon_pg.tmp'); print ""; exit;}?><b>page1.php</b><?session_start();$cur_user = $_SESSION["cur_user"];echo $_SESSION["test"];echo $cur_user->cur_role; // this echo display nothing, this is my problem?> Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63228 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 Sorry... but I dont see anywhere in your code where your setting any $_SESSION variables. Keep in mind (as I said many posts ago) session_register() has long been depricated.You might also want to look at using the header() function to force a redirect instead of uisng include() as some hack. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63234 Share on other sites More sharing options...
pixy Posted July 25, 2006 Share Posted July 25, 2006 [quote author=thorpe link=topic=101754.msg402966#msg402966 date=1153791940]Also... be aware that the use of [url=http://php.net/session_register]session_register[/url]() has long been depricated.[/quote]Thats what I was going to say. There's a new way of doing it now. You just have to set the session... Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63235 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 huh ? i did it there under index.php session_start();$cur_user = $_SESSION["cur_user"]; Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63236 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=pixy link=topic=101754.msg403004#msg403004 date=1153799578][quote author=thorpe link=topic=101754.msg402966#msg402966 date=1153791940]Also... be aware that the use of [url=http://php.net/session_register]session_register[/url]() has long been depricated.[/quote]Thats what I was going to say. There's a new way of doing it now. You just have to set the session...[/quote]i have to do it in the old way as i'm chaning people's coding. i cannot simply change it to the new syntax coz it will effect the whole system.any idea about it if i want to keep on with the old syntax? Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63237 Share on other sites More sharing options...
pixy Posted July 25, 2006 Share Posted July 25, 2006 I mean the if (session_is_registered) is associated with the session_register() function... you could just use if (isset($_SESSION['cur_user'])); instead...I dont know if it matters though. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63238 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 [quote]huh ? i did it there under index.phpsession_start();$cur_user = $_SESSION["cur_user"];[/quote]All that does is sets $cur_user to $_SESSION['cur_user']. Where do you assign a value to $_SESSION['cur_user']? Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63241 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 it doesn't work :( Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63245 Share on other sites More sharing options...
lilywong Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=thorpe link=topic=101754.msg403010#msg403010 date=1153800447][quote]huh ? i did it there under index.phpsession_start();$cur_user = $_SESSION["cur_user"];[/quote]All that does is sets $cur_user to $_SESSION['cur_user']. Where do you assign a value to $_SESSION['cur_user']?[/quote]I have already assign the $_SESSION['cur_user'] in index.php. but it still not working :( Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63246 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 [quote]it doesn't work[/quote]No kidding. Your missing the basics. Maybe go find a tutorial on how to use sessions. Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63247 Share on other sites More sharing options...
trq Posted July 25, 2006 Share Posted July 25, 2006 [quote]I have already assign the $_SESSION['cur_user'] in index.php. but it still not working[/quote]WHERE? POST THE RELEVENT CODE! Quote Link to comment https://forums.phpfreaks.com/topic/15553-php-session-help/#findComment-63249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.