konrad00 Posted May 6, 2009 Share Posted May 6, 2009 Hi, Problem: I want to show $_SESSION['myusername'] value on index.php. There is some condition in the top of the index.php. It does not work. I check whether sessions are passed at all between pages using these two scripts http://www.bigresource.com/PHP--_SESSION-not-working--0e0mvJoP.html I have following few scripts that upon succesful logon take me to index.php. Script is slightly modified based on http://www.phpeasystep.com/workshopview.php?id=6 tutorial. ---checklogin.php <?php session_start(); ob_start(); $host="localhost"; // Host name $username="kon"; // Mysql username $password="1"; // Mysql password $db_name="ajax"; // 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_register("myusername"); session_register("mypassword"); header("location:index.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> also here is main login file ----main_login.php <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> and finally my main page ----index.php <?php session_start(); error_reporting(5); $_SESSION['myusername']=$_POST['myusername']; if (!isset($_SESSION['myusername'])) { echo "not logged"; } else { $username = $_SESSION['myusername']; echo "Hello ". $myusername ; } phpinfo(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> html code goes here </html> Link to comment https://forums.phpfreaks.com/topic/157121-_session-not-passed-between-pages/ Share on other sites More sharing options...
w3evolutions Posted May 6, 2009 Share Posted May 6, 2009 session_register is being DEPRECATED soon so I would suggest just doing it the way you have in index.php. $_SESSION['myusername'] = $_POST['myusername']; Also this line on index.php $_SESSION['myusername']=$_POST['myusername']; tells php to reset the session variable. try this: if(isset($_POST)) && !empty($_POST['myusername'])) { $_SESSION['myusername']=$_POST['myusername']; } Link to comment https://forums.phpfreaks.com/topic/157121-_session-not-passed-between-pages/#findComment-827818 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 w3evolutions said most of it. You should also remove this line: $_SESSION['myusername']=$_POST['myusername']; Link to comment https://forums.phpfreaks.com/topic/157121-_session-not-passed-between-pages/#findComment-827819 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.