pkmleo Posted May 24, 2006 Share Posted May 24, 2006 Hi,I am working on a small website. I am using PHP 4.3.4, Apache 1.2, MySQL 5.0 on Win XP Pro. The login script works properly but session is not passed on to the next page. My code looks like this:<?phpsession_start();//header("Cache-control: private");require_once($_SERVER['DOCUMENT_ROOT'].'/sports/includes/db_config.php');$login_name = $_POST['login_name'];$password = $_POST['password'];$password = md5($password);$userIP = isset($REMOTE_HOST) ? $REMOTE_HOST : @gethostbyname($REMOTE_ADDR);$today = date("Y-m-d H:i:s");$sql = "SELECT * FROM admin WHERE login_name='$login_name' AND password='$password'";$query = "UPDATE admin SET last_login = '$today', ip_address = '$userIP' WHERE login_name = '$login_name'";$result = mysql_query($sql) or die("Unable to execute $sql query: " . mysql_error());$count = mysql_num_rows($result);if($count == 1){ //$_SESSION['login_name'] = $login_name; session_register('login_name'); $sessionId = session_id(); //print $_SESSION['login_name']; //session_write_close(); //echo $_SESSION['login_name']; header("Location: ./admin/adminhome.php?authenticateduser=$login_name?$sessionId"); //exit();}else{ header("Location: relogin.php");}?>I tried many options but still its not passing on the session to the next page. In the next page I am trying this:<?phpsession_start();//if(isset($_SESSION['login_name']))if(session_is_registered('login_name')){ //echo $_SESSION['login_name']; echo '$login_name'; //header("location:../index.php");}?>Its not printing the session. Any help with this will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/ Share on other sites More sharing options...
wildteen88 Posted May 24, 2006 Share Posted May 24, 2006 You are registering your sessions incorrectly. Scrap the sessin_register functions out and use this:[b]$_SESSION['login_name'] = $login_name;[/b]Then on your other page use:[b]print $_SESSION['login_name'];[/b]to get the login_name session.Also your script wont output anythink as you was not assigning yout login_name session with nothing. So when you went to the next page nothing was outputted as the login_name session didn't have any assigned to it. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38547 Share on other sites More sharing options...
pkmleo Posted May 24, 2006 Author Share Posted May 24, 2006 Still its not working. Its not printing the session. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38552 Share on other sites More sharing options...
wildteen88 Posted May 24, 2006 Share Posted May 24, 2006 Umm, OK create a new file called test.php and put the following into it[code]<?phpsession_start();$_SESSION['test'] = "Hello world";?><a href="test2.php">Test session</a>[/code]Now create a file called test2.php and put the following in it:[code]<?phpsession_start();echo $_SESSION['test'];?>[/code] Upload both files.And run test.php and click the [b]Test session[/b] link. It should take you to test2.php and hello world should be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38603 Share on other sites More sharing options...
pkmleo Posted May 25, 2006 Author Share Posted May 25, 2006 No its not displaying Hello World in test2.php page Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38866 Share on other sites More sharing options...
Crimpage Posted May 25, 2006 Share Posted May 25, 2006 Then your server is probably not creating the session files properly. Check the php.ini config, and make sure the tmp folder they are supposed to be written to has write access. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38867 Share on other sites More sharing options...
pkmleo Posted May 25, 2006 Author Share Posted May 25, 2006 I checked the tmp folder and I can see many session files being written in that folder. I dont understand this session thing in PHP. I did not experience this type of problems in JSP and ASP. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38878 Share on other sites More sharing options...
wisewood Posted May 25, 2006 Share Posted May 25, 2006 If you follow this correctly, and your sessions are working as they should, you will click the link to go to page2 and will be greeted with a welcome message.If the session is not working correctly, you will be told the session data didnt pass from the first page.create a file called page1.php & put the following code into it.[code]<?php session_start();$_SESSION[your_name] = "John Doe";echo "$_SESSION[your_name]<br>";echo "<a href=page2.php>Go to page 2</a>";?>[/code]Now create a file called page2.php with this code...[code]<?phpsession_start();if(!$_SESSION[your_name]){echo "The session variable did not pass from page1.php";}else{echo "Hello $_SESSION[your_name], welcome to page2.php";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38884 Share on other sites More sharing options...
pkmleo Posted May 25, 2006 Author Share Posted May 25, 2006 I did it and I get the message that The session variable did not pass from page1.php. So in this what is the problem. But the session did display the value in page1.php, but not in page2.php Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38885 Share on other sites More sharing options...
wisewood Posted May 25, 2006 Share Posted May 25, 2006 i know this is a stupid question, but you have got page1 and page2 on the same server haven't you? lol.I cant be of any more help, but at least you know where the problem lies now. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38892 Share on other sites More sharing options...
pkmleo Posted May 25, 2006 Author Share Posted May 25, 2006 Yes both the pages are in the same page. Do I need to make any changes in the php.ini file. The php.ini file is below:[Session]; Handler used to store/retrieve data.session.save_handler = files; Argument passed to save_handler. In the case of files, this is the path; where data files are stored. Note: Windows users have to change this ; variable in order to use PHP's session functions.session.save_path = C:\tmp; Whether to use cookies.session.use_cookies = 1; Name of the session (used as cookie name).session.name = PHPSESSID; Initialize session on request startup.session.auto_start = 0; Lifetime in seconds of cookie or, if 0, until browser is restarted.session.cookie_lifetime = 0; The path for which the cookie is valid.session.cookie_path = /; The domain for which the cookie is valid.session.cookie_domain =; Handler used to serialize data. php is the standard serializer of PHP.session.serialize_handler = php; Percentual probability that the 'garbage collection' process is started; on every session initialization.session.gc_probability = 1; After this number of seconds, stored data will be seen as 'garbage' and; cleaned up by the garbage collection process.session.gc_maxlifetime = 1440; Check HTTP Referer to invalidate externally stored URLs containing ids.; HTTP_REFERER has to contain this substring for the session to be; considered as valid.session.referer_check =; How many bytes to read from the file.session.entropy_length = 0; Specified here to create the session id.session.entropy_file =;session.entropy_length = 16;session.entropy_file = /dev/urandom; Set to {nocache,private,public} to determine HTTP caching aspects.session.cache_limiter = nocache; Document expires after n minutes.session.cache_expire = 180; trans sid support is disabled by default.; Use of trans sid may risk your users security. ; Use this option with caution.; - User may send URL contains active session ID; to other person via. email/irc/etc.; - URL that contains active session ID may be stored; in publically accessible computer.; - User may access your site with the same session ID; always using URL stored in browser's history or bookmarks.session.use_trans_sid = 0url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry" Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38894 Share on other sites More sharing options...
wildteen88 Posted May 25, 2006 Share Posted May 25, 2006 Okay. Seems all this boils down one thing! You have register globals on. If you get a result from this then you have register globals on!test.php[code]<?phpsession_start();session_register("hello");$hello = "Hello world";?><a href="test2.php">Test session</a>[/code]test2.php[code]<?phpsession_start();echo $hello;?>[/code]Also make sure your browser(s) accepts cookies!Do you get a result this time? Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38896 Share on other sites More sharing options...
kenrbnsn Posted May 25, 2006 Share Posted May 25, 2006 One more question ...Which web server are you using? If you're using IIS, I believe there are problems with IIS and sessions.Ken Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38899 Share on other sites More sharing options...
pkmleo Posted May 25, 2006 Author Share Posted May 25, 2006 Sorry, I am not getting the result. Its not displaying the session variable. I am not sure. Cookies are enabled in the browser.I am using PHP 4.2.3-win32, Apache 1.3, MySQL 5.0 on Win XP Pro. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-38900 Share on other sites More sharing options...
chai Posted May 26, 2006 Share Posted May 26, 2006 [!--quoteo(post=377011:date=May 25 2006, 08:15 AM:name=pkmleo)--][div class=\'quotetop\']QUOTE(pkmleo @ May 25 2006, 08:15 AM) [snapback]377011[/snapback][/div][div class=\'quotemain\'][!--quotec--]Sorry, I am not getting the result. Its not displaying the session variable. I am not sure. Cookies are enabled in the browser.I am using PHP 4.2.3-win32, Apache 1.3, MySQL 5.0 on Win XP Pro.[/quote]I am facing the exact same problem. I've tried reinstalling apache server, suspecting it is the cause.I have to revert back to a older version of phptriad before getting the session to work.Now it is working for me. Well, at least I am able to pass session variables between pages.Something is still notright though.Maybe you can try installing other version of apache or try with other installer. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-39252 Share on other sites More sharing options...
pkmleo Posted May 27, 2006 Author Share Posted May 27, 2006 Still nothing has changed. I removed PHP 4.2.3 and Apache 1.3 and installed PHP 4.4.2 and Apache 2.2, still session is not getting passed to the next page. I changed the session.save_path in the php.ini file but its not saving the session in the folder. The old version of PHP which I was using used to save session in that folder. I even checked the folder properties its not read-only also. I dont understand why this is happenning. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-39412 Share on other sites More sharing options...
chai Posted May 28, 2006 Share Posted May 28, 2006 well, also make sure that you have the statement session_start() at the very top(1st line) of each of your php pages. Even an empty space at the top of the page will result in that error. so, just place: [code]<? session_start()?>[/code]at the 1st line of your page.This certainly solves my problem. Hope it does for you.Maybe you'll want to have a look at the dedicated thread on "troubshooting session" in this forum. Quote Link to comment https://forums.phpfreaks.com/topic/10344-passing-session-from-one-page-to-another/#findComment-39583 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.