k3n3t1k Posted November 12, 2007 Share Posted November 12, 2007 I need some help in transferring my session from one page to the next. I know it has to do with the URL, but whenever I try it never fowards me. If anybody has a suggestion that would be great. Also referancing me to some tutorials would be great too! Thanks, k3n3t1k Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted November 12, 2007 Share Posted November 12, 2007 make sure you are putting session_start(); at the beginning of every page you want to use the session.. that should be all you need Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 12, 2007 Author Share Posted November 12, 2007 I already have the session start on the page. It still wont transfer. Here is the code, maybe this will help: <?php require_once "dbconfig.php"; $sql = mysql_query("SELECT * FROM studentcenter WHERE username='".addslashes($_POST['username'])."'") or die("Username is Incorrect ".mysql_error()); /* Check if Username Exists */ $results = mysql_fetch_array($sql) or die("Error at results section: ".mysql_error()); /* Puts database information into an array */ if($result['password'] == sha1($_POST['password'])) { /* Checks if passwords match */ session_start(); /* Start Session */ header ("Cache-control: private"); $_SESSION['sessionname'] = $_POST['username']; echo("Welcome, Forwarding"); header("location: studenthome.php?$_SESSION['sessionname']=1"); }else{ echo "Incorrect Login, please try again or <a href="register.php">Register</a>" } ?> Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted November 12, 2007 Share Posted November 12, 2007 I would take off the session you are passing to the url, and to the page that you are going to 'studenthome.php' ALSO needs to have session_start(); at the top of it... every page that uses session needs to ahve session_start(); Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 12, 2007 Author Share Posted November 12, 2007 I took off the Session from the URL and moved the session start to the top and it still didnt work. It doesnt seem to e fowarding past the login page to studenthome.php Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted November 12, 2007 Share Posted November 12, 2007 can you put the code to BOTH pages? Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 12, 2007 Author Share Posted November 12, 2007 Sure, here is login.php: <?php session_start(); require_once "dbconfig.php"; $sql = mysql_query("SELECT * FROM studentcenter WHERE username='".addslashes($_POST ['username'])."'") or die("Username is Incorrect ".mysql_error()); /* Check if Username Exists */ $results = mysql_fetch_array($sql) or die("Error at results section: ".mysql_error()); /* Puts database information into an array */ if($result['password'] == sha1($_POST['password'])) { /* Checks if passwords match */ /* Start Session */ header ("Cache-control: private"); $_SESSION['sessionname'] = $_POST['username']; echo("Welcome, Forwarding"); header("location: studenthome.php"); }else{ echo "Incorrect Login, please try again or <a href="register.php">Register</a>" } ?> and here is studenthome.php: <html> <head> <title>Gibson Partners -- Student Center</title> <link rel="stylesheet" type="text/css" href="css.css" /> </head> <body> <div id="top"> <?php include("leftnav.php"); ?> <div id="main"> <table width="90%"><tr><td> Hello <?php session_start(); if(!isset($SESSION["sessionname"])){ echo "You must be ogged in to do this!"; }else{ echo "Welcome ".$SESSION["sessionname"]; echo "YAY IT WORKED!!!"; echo "<a href="logout.php">Logout</a>"; } ?> </td></tr></table> <div id="right"> <?php include("right_border.php"); ?> </div> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted November 12, 2007 Share Posted November 12, 2007 theres the problem... session_start() has to be put at the top.. or BEFORE anything gets output to the browser meaning... there can NOT be ANY html or blank lines before the session_start. also its supposed to be $_SESSION not $SESSION.. see the difference? soo..... <? session_start(); ?> <html> <head> <title>Gibson Partners -- Student Center</title> <link rel="stylesheet" type="text/css" href="css.css" /> </head> <body> <div id="top"> <?php include("leftnav.php"); ?> <div id="main"> <table width="90%"><tr><td> Hello <?php if(!isset($_SESSION["sessionname"])){ echo "You must be ogged in to do this!"; }else{ echo "Welcome ".$_SESSION["sessionname"]; echo "YAY IT WORKED!!!"; echo "<a href="logout.php">Logout</a>"; } ?> </td></tr></table> <div id="right"> <?php include("right_border.php"); ?> </div> </div> </div> </body> </html> try that and see if that works for you Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 12, 2007 Author Share Posted November 12, 2007 Im sure that will help, but it still isnt transferring from the login.php to the studenthome.php I am pretty sure the problem is in there, but I cant tell where. Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted November 12, 2007 Share Posted November 12, 2007 that code works for me... try making some very basic pages... (no login script). page1 <? session_start(); $_SESSION['username'] = "MyName"; echo "Hello! ". $_SESSION['username']."<br />"; ?> <a href="page2.php">Click here to continue</a> page2 <? session_start(); echo "Hello ". $_SESSION['username']. echo "You made it to page 2"; ?> that code SHOULD work. if it doesnt... then there is a problem with your server/php/apache...something. if it does work.. then we know its one of the other pages.. something typed wrong. try on studenthome.php putting in at the bottom print_r($_SESSION); see what happens Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 13, 2007 Author Share Posted November 13, 2007 I used the code in the post above, and it fowarded, but i got no output Quote Link to comment Share on other sites More sharing options...
revraz Posted November 13, 2007 Share Posted November 13, 2007 You need to either check with your Webhost or if you have access to your PHP.INI file, that the Session Path is set correctly. Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 13, 2007 Author Share Posted November 13, 2007 OK, ill try that thanks Quote Link to comment Share on other sites More sharing options...
revraz Posted November 13, 2007 Share Posted November 13, 2007 Also, use <?PHP ?> instead of just <? ?> (I know you did in your original code, but some examples above used Short Tags which may not always be supported on your webhost). Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 13, 2007 Author Share Posted November 13, 2007 I did put <?php ?> on thier, it didnt help but had the same thoughts Quote Link to comment Share on other sites More sharing options...
revraz Posted November 13, 2007 Share Posted November 13, 2007 It wouldn't stop your sessions from working, I was just making you aware of it for your own use. Quote Link to comment 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.