urgent Posted February 26, 2007 Share Posted February 26, 2007 Anyone know how to use session? I wanted to do something like this: 1. User login in to the system. 2. System authenticate the user according to the username & password that the user have entered earlier. 3. System fetch the user_id of the user & store it in the session. 4. User insert the name, ic & master_id into the system. The master_id value will follow the id stored in the session variable. I use the coding below to authenticate the user and starts a session. <?php session_start(); $host="localhost"; // Host name $username="foong"; // Mysql username $password="216638"; // Mysql password $db_name="registration"; // Database name $tbl_name="user_info"; // Table name // Connect to server and select databse. $connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT id, username, password FROM $tbl_name WHERE username='$_POST[username]' and password='$_POST[password]'"; $result=@mysql_query($sql,$connection); // Mysql_num_row is counting table row $count=mysql_num_rows($result); $row = mysql_fetch_array($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername and redirect to file "login_success.php" $_SESSION['login'] = 1; $_SESSION['id']; header("location:ADMIN.php?id=$row[id]"); } else { $msg = " Problem login,please try again......."; header("location:message.php?msg=$msg"); } ?> As I read through the topic posted, I found out that session_register is no longer being used. How to pass the session from the first page until the final page? My session only applicable until the second page. What is the correct way to pass the session? <?php session_start(); ?> Is there anything that I need to specify to pass the session across pages? Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/ Share on other sites More sharing options...
magnetica Posted February 26, 2007 Share Posted February 26, 2007 You don't pass sessions across pages. When you add something to the session, it is there until either the session times out or the user closes the browser. So: session_start(); $_SESSION['variable'] = "test"; $_SESSION['variable'] will be stored in the session until as stated "the session times out or the user closes the browser. " Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/#findComment-194088 Share on other sites More sharing options...
urgent Posted February 26, 2007 Author Share Posted February 26, 2007 You don't pass sessions across pages. When you add something to the session, it is there until either the session times out or the user closes the browser. So: session_start(); $_SESSION['variable'] = "test"; $_SESSION['variable'] will be stored in the session until as stated "the session times out or the user closes the browser. " Thanks for your reply. The "variable" you specified in the coding is the name of the variable and the "test" is the value? Do I need to put this <?php session_start(); ?> at the heading of every page? Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/#findComment-194120 Share on other sites More sharing options...
kenrbnsn Posted February 26, 2007 Share Posted February 26, 2007 Yes, you need to put the "session_start()" function call before any output is sent to the screen on every page where you want to use sessions. Ken Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/#findComment-194123 Share on other sites More sharing options...
urgent Posted February 28, 2007 Author Share Posted February 28, 2007 Thanks for the reply. ;)I have tried the coding but the session can only be passed across the second page only. I will post the full coding that I have used later on as I don't have it with me now. Can I insert the session value into the table? Is this possible? This is the coding for page 1: <?php session_start(); $host="localhost"; // Host name $username="foong"; // Mysql username $password="216638"; // Mysql password $db_name="registration"; // Database name $tbl_name="user_info"; // Table name // Connect to server and select databse. $connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT id, username, password FROM $tbl_name WHERE username='$_POST[username]' and password='$_POST[password]'"; $result=@mysql_query($sql,$connection); // Mysql_num_row is counting table row $count=mysql_num_rows($result); $row = mysql_fetch_array($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername and redirect to file "login_success.php" $_SESSION['login'] = 1; $_SESSION['id']; //Is this correct? header("location:ADMIN.php?id=$row[id]"); } else { $msg = " Problem login,please try again......."; header("location:message.php?msg=$msg"); } ?> This is what I have in my third page: <?php session_start(); $master_id == "$_SESSION[id]"; //Is this correct? The value for the master_id will be the same as the id of the user .......... $sql = "INSERT into $tbl_name (name, grade, master_id) values("$_POST[name]", "$_POST[grade]", "what do I suppose to put here?")"; .......... ?> Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/#findComment-195807 Share on other sites More sharing options...
kenrbnsn Posted February 28, 2007 Share Posted February 28, 2007 Use one "=" in this line: <?php $master_id = $_SESSION['id']; ?> not two. Two equal signs are used for comparisons. Ken Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/#findComment-195816 Share on other sites More sharing options...
urgent Posted March 1, 2007 Author Share Posted March 1, 2007 This is what I want the system to do: 1. User login in to the system. 2. System authenticate the user according to the username & password that the user have entered earlier. 3. System fetch the user_id of the user & store it in the session. 4. User insert the name, ic & master_id into the system. The master_id value will follow the id stored in the session variable. coding for login.php <html> <head></head> <body> <table border="0" width="694" height="292" cellspacing="0"> <b><font size="5">LOGIN PAGE</font></b><br><hr></tr> <center> <br><br><td width="1000" height="226" align="center" valign="top"> <table width="300" height="50" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <td colspan="3"><strong><center><font size = "5">ADMIN LOGIN</font></center></strong></td> </tr> <tr> <form method="post" action="CHECKLOGIN.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="4" bgcolor="#FFFFFF"> <tr> <td width="78">USERNAME</td> <td width="6">:</td> <td width="294"><input name="username" type="text" ></td> </tr> <tr> <td>PASSWORD</td> <td>:</td> <td><input name="password" type="password" ></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </body> </html> coding for checkLogin.php <?php session_start(); $host="localhost"; // Host name $username="foong"; // Mysql username $password="216638"; // Mysql password $db_name="registration"; // Database name $tbl_name="user_info"; // Table name // Connect to server and select databse. $connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT id, username, password FROM $tbl_name WHERE username='$_POST[username]' and password='$_POST[password]'"; $result=@mysql_query($sql,$connection); // Mysql_num_row is counting table row $count=mysql_num_rows($result); $row = mysql_fetch_array($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername and redirect to file "login_success.php" $_SESSION['login'] = 1; $_SESSION['id']; header("location:INSERT_RECORD.php?id=$row[id]"); } else { $msg = " Problem login,please try again......."; header("location:message.php?msg=$msg"); } coding for insert_record.php <?php session_start(); if(isset($_SESSION['login'])) echo ""; else { header("location: LOGIN.php"); } $id == $_SESSION['id']; echo "ID = $id"; ?> <html> <head></head> <body> <table border="0" width="694" height="292" cellspacing="0"> <b><font size="5">INSERT PAGE</font></b><br><hr></tr> <center> <br><br><td width="1000" height="226" align="center" valign="top"> <table width="300" height="50" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <td colspan="3"><strong><center><font size = "5">INSERT PAGE</font></center></strong></td> </tr> <tr> <form method="post" action="doINSERT_DETAILS.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="4" bgcolor="#FFFFFF"> <tr> <td width="78">NAME</td> <td width="6">:</td> <td width="294"><input name="name" type="text" ></td> </tr> <tr> <td>GRADE</td> <td>:</td> <td><input name="grade" type="text" ></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="SUBMIT"></td> </tr> </table> </td> </form> </body> </html> coding for doinsert_details.php <? session_start(); $id == $_SESSION['id']; echo "ID = $id"; $host="localhost"; // Host name $username="TEST"; // Mysql username $password="TEST"; // Mysql password $db_name="registration"; // Database name $tbl_name="bhgn1"; // table name // Connect to server and select databse. $connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect"); $db=@mysql_select_db("$db_name")or die("cannot select DB"); $master_id = $_SESSION[id]; $sql="INSERT INTO $tbl_name (master_id,name,grade) VALUES ('$_SESSION[id]','$_POST[name]','$_POST[grade]')"; $result=@mysql_query($sql,$connection) or die(mysql_error()); ?> <head></head> <body> <center> <table border="0" width="694" height="292" cellspacing="0"> <form method="post" action="INSERT_MAIN.php"> <tr> <td width="1000" height="226" valign="top"><br> <b><font size="5">NEW RECORD ADDED INTO <? echo "$tbl_name" ?></font></b><br><hr> <br><br><b><? echo "$_SESSION[id]</b> has been inserted into <b>$tbl_name</b>";?><br><br><br> <INPUT TYPE="SUBMIT" NAME="submit" VALUE="INSERT ANOTHER RECORD"> </table> </body> </html> Anyone have any idea what is wrong with the codes? ?> Quote Link to comment https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/#findComment-196575 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.