brad_langdon Posted May 10, 2009 Share Posted May 10, 2009 I have a process page that logs the user in...no problems there. I have two session variables but only one is passing onto the next page. // PROCESS PAGE if($count==1){ $_session['client'] = $myusername; // myusername is already defined from a form $_session['login'] = 'TRUE'; // this variable passes through fine session_write_close(); header("location:http://www.vander.co.nz/login/loggedin.php"); } // NEXT PAGE <? if ($_session['login'] = 'TRUE') { echo ("Welcome"); } $result = $_session['client']; // supposed to echo the clients name stored in "$myusername" echo $result; ?> The end result does echo the "Welcome" but not the client name. I have included a start session at the top of both pages and also ended the session before the header on the process page. Really stuck and have researched with no success. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/ Share on other sites More sharing options...
Maq Posted May 10, 2009 Share Posted May 10, 2009 You need session_start at the top of every page you use sessions. Please use around code for proper formatting and syntax highlighting. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830825 Share on other sites More sharing options...
PFMaBiSmAd Posted May 10, 2009 Share Posted May 10, 2009 Does your actual code use $_SESSION or does it use $_session? $_SESSION is a session variable name. $_session is just a local variable in your code. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830834 Share on other sites More sharing options...
WolfRage Posted May 10, 2009 Share Posted May 10, 2009 Just one more, to make sure you do not have a garbage in garbage out scenario,are you sure that $myusername is really set to something. However I would say that PFMaBiSmAd Has identified a major issue for you. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830840 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 Thanks for the reply. I changed it to SESSION instead of session but still no luck. $myusername has been set as it is the input from the form on a previous page. I added ini_set ("display_errors", "1"); error_reporting(E_ALL); to the output page but nothing came up. PS I am not a pro with php... Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830865 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 You need session_start at the top of every page you use sessions. ^ Read. Did you do that? Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830867 Share on other sites More sharing options...
PFMaBiSmAd Posted May 10, 2009 Share Posted May 10, 2009 The following line is not testing if the value is equal to 'TRUE' it is setting the variable listed to the sting 'TRUE' and testing if the result of that is true, which it is - if ($_session['login'] = 'TRUE') { echo ("Welcome"); } An equal comparison is two == signs. One = sign is an assignment operator. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830871 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 Yes I have started a session on both pages. Thanks I have changed the first variable call to == instead of =. Still no luck with the second variable call...so confused ??? Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830873 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Post your updated code. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830876 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 Now that I checked the first variable isn't working either...I changed the variable name in the process page so that it didn't match but it still prints as true on the results page. Thank you so much for the help by the way... PROCESS PAGE <?php session_start(); ob_start(); $host="****"; // Host name $username="****"; // Mysql username $password="****"; // Mysql password $db_name="login"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['client'] = $myusername; $_SESSION['login'] = "TRUE"; session_write_close(); header("location:http://www.vander.co.nz/login/loggedin.php"); } else { $_SESSION['login'] == "TRUE"; session_write_close(); header("Location: http://www.vander.co.nz/index.php"); } ob_end_flush(); ?> END PAGE <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <link href="http://www.vander.co.nz/Images/Favicon.ico" rel="shortcut icon" /> <link href="http://www.vander.co.nz/css/main.css" rel="stylesheet" type="text/css" /> <link href="http://www.vander.co.nz/css/scroll.css" rel="stylesheet" type="text/css" /> <link href='http://www.vander.co.nz/css/contact.css' type='text/css' rel='stylesheet' media='screen' /> <link href="http://www.vander.co.nz/css/prettyPhoto.css" rel="stylesheet" type="text/css" media="screen" charset="utf-8" /> <script src="http://www.vander.co.nz/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script src="http://www.vander.co.nz/js/jquery.pngFix.js" type="text/javascript" ></script> <script src="http://www.vander.co.nz/js/jquery.scrollable-1.0.2.js" type="text/javascript" ></script> <script src="http://www.vander.co.nz/SpryAssets/SpryEffects.js" type="text/javascript"></script> <script src='http://www.vander.co.nz/js/jquery.simplemodal.js' type='text/javascript'></script> <script src="http://www.vander.co.nz/js/contact.js" type='text/javascript'></script> <meta http-equiv="Content-Type" ncontent="text/html; charset=utf-8" /> <title>Vander - Branding & Design</title> <script type="text/javascript"> <!-- function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];} } function MM_effectAppearFade(targetElement, duration, from, to, toggle) { Spry.Effect.DoFade(targetElement, {duration: duration, from: from, to: to, toggle: toggle}); } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} } //--> </script> </head> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-8299099-1"); pageTracker._trackPageview(); } catch(err) {}</script> <body id="Home" onload="MM_preloadImages('Images/Portfolio/PrevOver.png','Images/Portfolio/NextOver.png')"> <?php include("../header.php"); ?> <div id="content"> <? if ($_SESSION['login'] == "TRUE") { echo ("Welcome"); } $result = $_SESSION['client']; echo $result; ?> <div style="width:100%; height:56px; float:left;"></div> </div> <?php include("../footer.php"); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830880 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 1. What's with ob_start() and ob_end_flush()? 2. Change the PHP tag <? to <?php in END PAGE. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830884 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 I don't know what those two tags are for as this is modified downloaded code. Should I take them off? I have now changed the <? to <?php still no luck though. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830885 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Remove those two functions - ob_start() and ob_end_flush(). Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830887 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 I removed them...still not working properly. Have I typed the variable in the wrong syntax or something? Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830889 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Which page does the process page redirect you to? And what page is the END PAGE? Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830893 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 It redirects you to the end page. the end page is called loggedin.php. There are two pages of concern. The process page and the end page which is called loggedin.php Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830897 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Before the header that redirects you to loggedin.php, can you put: var_dump($_SESSION['client']); Then comment out the header so it doesn't redirect you before you can see that output. Post that output here. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830899 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 The output is...string(6) "knight" knight is the user name so that part worked...not sure what string(6) is though? Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830903 Share on other sites More sharing options...
PFMaBiSmAd Posted May 10, 2009 Share Posted May 10, 2009 Add the following two lines of code immediately after your first opening <?php tag on both the page where you are setting the session variables and on the page where you are referencing the session variables - ini_set ("display_errors", "1"); error_reporting(E_ALL); If you already have some existing php code immediately after the opening <?php tag, move that code down to its own line and add the above two lines immediately after the <?php tag. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830904 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 string(6) just means the type of the variable is a string and it has length 6. Now you can take that line out and uncomment header. Thanks! After that, in your END PAGE, can you modify the first line from: <?php session_start(); ?> with <?php session_start(); var_dump($_SESSION['client']); ?> Same deal. Post what it prints out. Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830905 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 Add the following two lines of code immediately after your first opening <?php tag on both the page where you are setting the session variables and on the page where you are referencing the session variables - ini_set ("display_errors", "1"); error_reporting(E_ALL); If you already have some existing php code immediately after the opening <?php tag, move that code down to its own line and add the above two lines immediately after the <?php tag. That made no difference... and for string(6) just means the type of the variable is a string and it has length 6. Now you can take that line out and uncomment header. Thanks! After that, in your END PAGE, can you modify the first line from: <?php session_start(); ?> with <?php session_start(); var_dump($_SESSION['client']); ?> Same deal. Post what it prints out. the output was "NULL" Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830912 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 Update to last post... PFMaBiSmAd, the last piece of code yo asked me to write came up with this output... "Notice: Undefined index: client in /var/www/vhosts/vander.co.nz/httpdocs/login/loggedin.php on line 76" The code on line 76 is if ($_SESSION['login'] == "TRUE") { echo ("Welcome"); } Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-830921 Share on other sites More sharing options...
nrg_alpha Posted May 10, 2009 Share Posted May 10, 2009 I added ini_set ("display_errors", "1"); error_reporting(E_ALL); to the output page but nothing came up. I would get into the habit of also including E_STRICT (to catch any strict standards notices as well): error_reporting(E_ALL | E_STRICT); (I'm guessing not related to the problem at hand..it's just nice to use, just in case of any other possible strict standard issues). Side note: In one of the snippets (in the PROCESS PAGE), looks like you provided your actual database username and password.. I would recommend just substituting those values with asterisks or something. Shouldn't post the actual values... Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-831022 Share on other sites More sharing options...
brad_langdon Posted May 10, 2009 Author Share Posted May 10, 2009 Thanks. Does anyone out there have any idea why this is not working though??? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/157561-session-variables-issue/#findComment-831214 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.