wmguk Posted August 19, 2007 Share Posted August 19, 2007 Hey, Firstly, im sort of a newbie, but i roughly know what im doing, im currently working on a project, which at the moment is 150 pages,... I have a login script then i have a session header on each page, what i want to do is have a session include on each page, so if i change the session info, it is changed on every page.. something like this <? include sess.php ; ?> the sess.php is <? session_start(); $t = date("h:i:s", time()); $_SESSION['admin_login_time'] = $t; $_SESSION['myusername'] = $user; if(!session_is_registered(myusername)){ header("location:index.php"); } ?> now, also, $user is not recognised. this is my login page <?php include "connection.php"; // Connect to server and select databse. if (!$con) { die('Could not connect: ' . mysql_error() ); } mysql_select_db($db, $con); // username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM our_info 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"); header("location:../main.php"); } else { header("location:../failed.html"); } ?> but for some reason $user isnt saved Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/ Share on other sites More sharing options...
NArc0t1c Posted August 19, 2007 Share Posted August 19, 2007 Firstly, code tags. If a session is changed it will stay changed on all pages. Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328170 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 Firstly, code tags. If a session is changed it will stay changed on all pages. Ok, im gonna sound really dumb now, what do you mean by code tags? can you show me a small example of what u mean? sorry to be stupid Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328172 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 any help would be cool ? Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328210 Share on other sites More sharing options...
plutomed Posted August 19, 2007 Share Posted August 19, 2007 When you create a session normally that last until the browser window is closed I think so you don't have to put it at the top of every page Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328215 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 oh ok, any thought why i cant see $user? Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328232 Share on other sites More sharing options...
plutomed Posted August 19, 2007 Share Posted August 19, 2007 You haven't done what I did and defined it somewhere else have you? Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328234 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 I dont think so, basically, people go from this page <?php include "connection.php"; // Connect to server and select databse. if (!$con) { die('Could not connect: ' . mysql_error() ); } mysql_select_db($db, $con); // username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM our_info 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[myusername] = $_POST[myusername]; header("location:../main.php"); } else { header("location:../failed.html"); } ?> to this page: <? session_start(); $_SESSION["myusername"] if(!session_is_registered(myusername)){ header("location:index.php"); } ?> //some html missing <? echo $username; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328235 Share on other sites More sharing options...
NArc0t1c Posted August 19, 2007 Share Posted August 19, 2007 Code Tags: [ code][/ code] or the # button. Where are you including the sessions script? Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328255 Share on other sites More sharing options...
plutomed Posted August 19, 2007 Share Posted August 19, 2007 Where have you set $username? Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328260 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 Code Tags: [ code][/ code] or the # button. Where are you including the sessions script? oh ok, you mean on the forum - sorry i used the [ PHP ] tags, to display what i was doing.... according to a tutorial website, it says that this <?php session_start(); if(!session_is_registered(myusername)){ header("location:index.php"); } ?> needs to be on the top of the page... <?php session_start(); if(!session_is_registered(myusername)){ header("location:index.php"); } ?> <!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"><!-- InstanceBegin template="/Templates/admin.dwt" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title> Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328261 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 Ok, ive just taken all the info directly from the tutorial website.. index.php: <form action="scripts/login.php" method="post" name="login"> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" valign="middle" class="main"> </td> <td align="center" valign="middle"> </td> </tr> <tr> <td align="center" valign="middle" class="main"><div align="center">Username</div></td> <td align="center" valign="middle"> <div align="center"> <input name="myusername" type="text" class="main" id="myusername" /> </div></td> </tr> <tr> <td align="center" valign="middle" class="main"><div align="center">Password</div></td> <td align="center" valign="middle"><div align="center"> <p> <input name="mypassword" type="password" class="main" id="mypassword" /> </p> </div></td> </tr> <tr> <td align="center" valign="middle"><div align="center"></div></td> <td align="center" valign="middle"><div align="center"></div></td> </tr> <tr> <td colspan="2" align="center" valign="middle"> <div align="center"> <br /> <input type="submit" class="main" name="Submit" value="Log In" /> </div></td> </tr> </table> </form> login.php: <?php include "connection.php"; // Connect to server and select databse. if (!$con) { die('Could not connect: ' . mysql_error() ); } mysql_select_db($db, $con); // username and password sent from signup form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; $sql="SELECT * FROM our_info 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:../main.php"); } else { header("location:../failed.html"); } ?> main.php: <?php session_start(); if(!session_is_registered(myusername)){ header("location:index.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328266 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 i've tried to echo $user, $username and $my username... i also tried <?php echo $_SESSION['myusername'] ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328271 Share on other sites More sharing options...
NArc0t1c Posted August 19, 2007 Share Posted August 19, 2007 uhm, try this following, it has all those grouped as one. <?php //Engage sesions. session_start(); /* Here we include the mysql connection script. */ include("connection.php"); // Connect to server and select databse. if (!$con) { die('Could not connect: ' . mysql_error()); } else { if (!mysql_select_db($db, $con)){ echo 'Could not connect to database.'; exit; } } if(isset($_POST['Submit'])){ $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; /* Select data from database. */ $sql="SELECT * FROM our_info WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $count=mysql_num_rows($result); /* If only one occurrence is there. */ if($count==1){ /* Set the myusername sesion to 1 */ $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; /* Make an sesion called authenticated to tell main.php that the user is logged in. */ $_SESSION['authenticated'] == 'true'; echo 'Login has succeeded.<br />Click <a href="main.php">here</a> to continue'; exit; } else { echo 'Failed to login.'; exit; } } else { ?> <form action="scripts/login.php" method="post" name="login"> <table width="500" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" valign="middle" class="main"> </td> <td align="center" valign="middle"> </td> </tr> <tr> <td align="center" valign="middle" class="main"><div align="center">Username</div></td> <td align="center" valign="middle"> <div align="center"> <input name="myusername" type="text" class="main" id="myusername" /> </div></td> </tr> <tr> <td align="center" valign="middle" class="main"><div align="center">Password</div></td> <td align="center" valign="middle"><div align="center"> <p> <input name="mypassword" type="password" class="main" id="mypassword" /> </p> </div></td> </tr> <tr> <td align="center" valign="middle"><div align="center"></div></td> <td align="center" valign="middle"><div align="center"></div></td> </tr> <tr> <td colspan="2" align="center" valign="middle"> <div align="center"> <br /> <input type="submit" class="main" name="Submit" value="Log In" /> </div></td> </tr> </table> </form> <?php } ?> Edit: Please note, I haven't tested this yet. Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328277 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 ok, wowzers thank you - that worked... now, how to i set $session[$myusername] = $user so that i dont need to use: <?php echo ' '.$_SESSION['myusername']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328281 Share on other sites More sharing options...
NArc0t1c Posted August 19, 2007 Share Posted August 19, 2007 How do you mean? On the script where you want to use the user variable use the following, <?php /* Check for the authenticated session. */ if(isset($_SESSION['authenticated'])){ /* make the user variable. */ $user = $_SESSION['myusername']; } else { /* Echo a 401 error, in this case, echo message */ echo 'You are not authenticated.'; exit; } /* The rest of the script would come here. */ ?> Edit: Added Comments. Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328282 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 hmm, ok, just had a play : <?php session_start(); if(isset($_SESSION['authenticated'])){ $user = $_SESSION['myusername']; } else { header("location:index.php"); exit; } ?> I have a location section in there, so that if the login fails, it will redirect back to index.php.. the only problem is that, it fails everytime now.. have I done it right? Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328284 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 is there a difference between: $user = $_SESSION['myusername']; and $_SESSION['myusername'] = $user; Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328287 Share on other sites More sharing options...
wmguk Posted August 19, 2007 Author Share Posted August 19, 2007 P.S, thanks so much for all your help, you've all be marvellous so far Cheeeeerrrrssss Quote Link to comment https://forums.phpfreaks.com/topic/65705-solved-daft-session-question-help-please-plus-other-session-help-required/#findComment-328308 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.