k3n3t1k Posted November 14, 2007 Share Posted November 14, 2007 Could you look at this code and tell me what you think would cause the page not to foward to the header? <?php session_start(); ?> <html> <head> <title> Login </title> <body> <?php require_once "dbconfig.php"; $sql = mysql_query("SELECT * FROM studentcenter WHERE username='"($_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'] == $_POST['password']) { /* Checks if passwords match */ /* Start Session */ header ("Cache-control: private"); $SESSION['sessionname'] = $_POST['username']; session_name($SESSION['sessionname); session_id([id]); echo("Welcome, Forwarding"); header("location: studenthome.php?id") or die("Will not foward" .mysql_error()); }else{ echo "Incorrect Login, please try again or <a href="register.php">Register</a>"; } ?> </body> </html> Thanks, k3n3t1k Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 14, 2007 Share Posted November 14, 2007 <?php header ("Cache-control: private"); $SESSION['sessionname'] = $_POST['username']; session_name($SESSION['sessionname']); session_id([id]); echo("Welcome, Forwarding"); header("location: studenthome.php?id") or die("Will not foward" .mysql_error()); }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...
roopurt18 Posted November 14, 2007 Share Posted November 14, 2007 Well, first off you have a syntax error that darkfreaks fixed. Secondly, you're using the wrong variable for sessions; try $_SESSION instead of $SESSION. Thirdly, you are echo'ing output before your call to header(). You must never do this. Headers have to be sent before any output; nothing can come before headers, not even white space. You'll have to restructure your logic to fix that. Fourth, if you want to redirect with a header('Location: ...') then you must call exit(); afterwards or your script will keep processing. Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 14, 2007 Author Share Posted November 14, 2007 Is this what it should look like after those errors? <?php header ("Cache-control: private"); $_SESSION['sessionname'] = $_POST['username']; session_name($SESSION['sessionname']); session_id([id]); header("location: studenthome.php?id"); exit(); echo("Welcome, Forwarding"); or die("Will not foward" .mysql_error()); }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...
darkfreaks Posted November 14, 2007 Share Posted November 14, 2007 <?php header ("Cache-control: private"); $_SESSION['sessionname'] = $_POST['username']; session_name($_SESSION['sessionname']); session_id([id]); header("location: studenthome.php?id"); exit(); echo("Welcome, Forwarding"); or die("Will not foward" .mysql_error()); }else{ echo "Incorrect Login, please try again or <a href="register.php">Register</a>"; } ?> still forgot the underscore in session Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 14, 2007 Share Posted November 14, 2007 <?php header ("Cache-control: private"); $_SESSION['sessionname'] = $_POST['username']; // In the following line you may mean $_SESSION['sessionname'], although I'm not sure session_name($SESSION['sessionname']); // What in the Hell is [id]? session_id([id]); // It doesn't look to me as if you finished your query string...?id=the_id header("location: studenthome.php?id"); exit(); // Next line is pointless, you'll have already exited and redirected (you'll never see the message) echo("Welcome, Forwarding"); // What is this or die() attached to? or die("Will not foward" .mysql_error()); // Is there more code above this? or do you just have a random }else{ floating around }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...
k3n3t1k Posted November 14, 2007 Author Share Posted November 14, 2007 I tried the suggestions above to no avail. I probabbly should have posted this before but there is more code, here is the entire page: <?php session_start(); ?> <html> <head> <title> Login </title> <body> <?php require_once "dbconfig.php"; $sql = mysql_query("SELECT * FROM studentcenter WHERE username='"($_POST['username'])."'"); /* Check if Username Exists */ $results = mysql_fetch_array($sql); /* Puts database information into an array */ if($result['password'] == $_POST['password']) { /* Checks if passwords match */ /* Start Session */ header ("Cache-control: private"); $_SESSION['sessionname'] = $_POST['username']; session_name($_SESSION['sessionname']); session_id(1); header("location: studenthome.php?id=1"); exit(); }else{ echo "Incorrect Login, please try again or <a href="register.php">Register</a>"; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 14, 2007 Share Posted November 14, 2007 You are sending output before your calls to header(). <?php session_start(); ?> <html> <head> <title> Login </title> <body> That markup counts as output. I already told you that you need to restructure your code to avoid that. Quote Link to comment Share on other sites More sharing options...
k3n3t1k Posted November 14, 2007 Author Share Posted November 14, 2007 I put the both headers before anything. It still wont foward. <?php header("location: studenthome.php?id=1"); header ("Cache-control: private"); session_start(); require_once "dbconfig.php"; $sql = mysql_query("SELECT * FROM studentcenter WHERE username='"($_POST['username'])."'"); /* Check if Username Exists */ $results = mysql_fetch_array($sql); /* Puts database information into an array */ if($result['password'] = $_POST['password']) { /* Checks if passwords match */ /* Start Session */ $_SESSION['sessionname'] = $_POST['username']; session_name($_SESSION['sessionname']); session_id(1); /* header("location: studenthome.php?id=1"); */ exit(); }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...
roopurt18 Posted November 14, 2007 Share Posted November 14, 2007 Try this: <?php header('Location: http://www.google.com/'); exit(); ?> Confirm that it works in the most simple case. 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.