jscix Posted February 13, 2007 Share Posted February 13, 2007 Ok, my question is: How do I stop the content of a script from loading and instead; redirect to another page based on criteria performed in a seperate script, that is php included from within the original script? The problem is, the included script which supplies the criteria for whether or not the content should load, must be included after some session, and error handeling code.. which prevents me from redirecting, and as far as I am aware, prevents the code ABOVE the INCLUDE, from being able to read any variables that are set within the included file, since they have no value until the included script sets them. Is there any way around this, or am I going to have to redesign the way the script works?? Here is the login script - Which at the bottom includes display.php <?php session_start(); $xxxy = (isset($_SESSION['userrname'])); if ($xxxy == "1") { include ('db_connect.php'); $connection1 = mysql_connect($db_host, $db_username, $db_password); if (!$connection1) { die ("Could not connect to the database: <br />" . mysql_error()); } $db_select = mysql_select_db($db_database); if (!$db_select) { die ("Could not select the database: <br />" . mysql_error()); } $uhusr = ($_SESSION['userrname']); $sql2 = "SELECT * FROM `" . $uhusr . "` LIMIT 0, 30 "; $duh2 = mysql_query($sql2); $res2 = mysql_fetch_row(($duh2)); $latl = ($_SESSION['passwordd']); if ($res2[0] == $latl) { print "<center />You are already logged in <a href=index.php />click here</a /><br />"; die (); } if (!duh2) { die ("Username does not exist."); } } else { } ?> <html> <center> <img src="/PHP/Test/login.jpg"><br> <?php include "forms.php"; $ohno2 = include ("parse.php"); $dbusr1 = ($_POST["usrfeild"]); $dbpw1 = ($_POST["pwfeild"]); // Validatestr is called from within parse.php $usrf1 = Validatestr($dbusr1); $pwf1 = Validatestr($dbpw1); if ($dbusr1 == (null)) { print "Please enter a username and password to continue."; die (); } elseif ($dbpw1 == (null)) { print "Please enter a username and password to continue."; die (); } elseif ($usrf1 == "") { print $usrf1 . "<br />"; print "Usernames must start with a letter!"; die (); } elseif ($pwf1 == "") { print $pwf1 . "<br />"; print "Passwords must start with a letter!"; die (); } elseif ((strlen($dbusr1) < 3)) { print "Usernames must be at least 3 characters."; die (); } elseif ((strlen($dbpw1) < 3)) { print "Passwords must be at least 3 characters."; die (); } else { include "display.php"; } ?> </center> </html> Here is the display.php script -- At the bottom is a print "thanks for logging in click here" message, Instead of this I would like to either redirect or set a variable that can be read from the loginscript and redirect from there... <?php include ('db_connect.php'); $connection = mysql_connect($db_host, $db_username, $db_password); if (!$connection) { die ("Could not connect to the database: <br />" . mysql_error()); } $db_select = mysql_select_db($db_database); if (!$db_select) { die ("Could not select the database: <br />" . mysql_error()); } $sql = "SELECT * FROM `" . $dbusr1 . "` LIMIT 0, 30 "; $duh = mysql_query($sql); if (!$duh) { die ("Username does not exist."); } $result = mysql_fetch_row(($duh)); $retpass = $result[0]; $pwmd5 = (md5($dbpw1)); $name1 = $pwmd5; $name2 = $retpass; $result = strcasecmp($name1, $name2); if (!$result) { $_SESSION['userrname'] = $dbusr1; $_SESSION['passwordd'] = $pwmd5; print "<center />Thanks for logging in $sbusr1 <a href=index.php />click here</a />"; mysql_close(); die (); } else { echo "You have entered an incorrect password."; mysql_close(); die (); } mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
fert Posted February 13, 2007 Share Posted February 13, 2007 header("Location: http://www.somesite.com/somepage.php"); exit; Quote Link to comment Share on other sites More sharing options...
jscix Posted February 13, 2007 Author Share Posted February 13, 2007 That will produce an error since I have to start a session first, to check if they are logged in before redirecting, no? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 No. Quote Link to comment Share on other sites More sharing options...
fert Posted February 13, 2007 Share Posted February 13, 2007 http://us2.php.net/manual/en/function.ob-start.php that will stop any errors Quote Link to comment Share on other sites More sharing options...
jscix Posted February 13, 2007 Author Share Posted February 13, 2007 Sigh, still getting this: Warning: Cannot modify header information - headers already sent by (output started at login.php:56 in display.php on line 46 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 Then you're not only setting the session - what's on line 56 of login.php? Quote Link to comment Share on other sites More sharing options...
jscix Posted February 13, 2007 Author Share Posted February 13, 2007 Sorry I posted that before seeing ferts post on ob_start(), it fixed the problem, thanks much both of you. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 13, 2007 Share Posted February 13, 2007 ob_start and ob_end_flush(); are not a solved problam really you need to find out why theres an error ok? I reacon that you have got html before the header infromation ok. good luck ps.All you need to remember is that ob_start is not the best statement to stop header errors ok. 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.