BluMess Posted May 9, 2008 Share Posted May 9, 2008 Heya, I'm new here Anyway, I've started up a website and I'm trying to implement a feature where you log in and you get sent to another page if it works properly. I've got the login code which works perfectly, but when I try to put the redirection code in [ header("Location:blahblah") ] it just gives me a blank page. this is the code: <?php // login2.php include("connection.php"); // Start a session. Session is explained below. session_start(); // Same checking stuff all over again. if(isset($_POST['submit'])) { if(empty($_POST['username']) || empty($_POST['password'])) { echo "Sorry, you have to fill in all forms"; exit; } // Create the variables again. $username = $_POST['username']; $password = $_POST['password']; // Encrypt the password again with the md5 hash. // This way the password is now the same as the password inside the database. $password = md5($password); // Store the SQL query inside a variable. // ONLY the username you have filled in is retrieved from the database. $query = "SELECT username,password FROM `users` WHERE username='$username'"; $result = mysql_query($query); if(!$result) { // Gives an error if the username given does not exist. // or if something else is wrong. echo "The query failed " . mysql_error(); } else { // Now create an object from the data you've retrieved. $row = mysql_fetch_object($result); // You've now created an object containing the data. // You can call data by using -> after $row. // For example now the password is checked if they're equal. if($row->password != $password) { echo "The chat service is in a state of dis-repair. Well the login box is, to be honest. Darn it, I knew I shouldn't have pressed that big red button!"; exit; } // By storing data inside the $_SESSION superglobal, // you stay logged in until you close your browser. $_SESSION['username'] = $username; $_SESSION['sid'] = session_id(); // Make it more secure by storing the user's IP address. $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; header("Location:http://www.google.com"); ?> } } Yeah. the part which causes the trouble as far as I know is the redirect part - header("Location:http://www.google.com"); Obviously I don't want it to go to google, but it's just an example. I really hope you guys can help - I managed to get it working once, by rejiggling the php tags about but I lost that and now it doesn't work Any suggestions? Thanks, Chris Link to comment https://forums.phpfreaks.com/topic/104905-redirection-help/ Share on other sites More sharing options...
947740 Posted May 9, 2008 Share Posted May 9, 2008 You probably have errors suppressed. The header must be before anything is echoed. There can be no content whatsoever before the header is sent: no echos, no html, nothing. Link to comment https://forums.phpfreaks.com/topic/104905-redirection-help/#findComment-536928 Share on other sites More sharing options...
BluMess Posted May 9, 2008 Author Share Posted May 9, 2008 I know, but is there any way I can make it redirect? Maybe using another file or something? Link to comment https://forums.phpfreaks.com/topic/104905-redirection-help/#findComment-536944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.