March 26, 200917 yr Hey guys, I have setup my database, table and register form. When I log-in everything is fine...however the page stays on login.php?op=login and doesn't redirect to members.php I have included my code below to see if you can spot any errors. Cheers for your help! Josh <?php session_start(); // dBase file include "dbConfig.php"; if ($_GET["op"] == "login") { if (!$_POST["username"] || !$_POST["password"]) { die("You need to provide a username and password."); } // Create query $q = "SELECT * FROM `dbUsers` " ."WHERE `username`='".$_POST["username"]."' " ."AND `password`=PASSWORD('".$_POST["password"]."') " ."LIMIT 1"; // Run query $r = mysql_query($q); if ( $obj = @mysql_fetch_object($r) ) { // Login good, create session variables $_SESSION["valid_id"] = $obj->id; $_SESSION["valid_user"] = $_POST["username"]; $_SESSION["valid_time"] = time(); // Redirect to member page Header("Location: members.php"); } else { // Login not successful die("Sorry, could not log you in. Wrong login information."); } } else { //If all went right the Web form appears and users can log in echo "<form action=\"?op=login\" method=\"POST\">"; echo "Username: <input name=\"username\" size=\"15\"><br />"; echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />"; echo "<input type=\"submit\" value=\"Login\">"; echo "</form>"; } ?>
March 26, 200917 yr Using the header(...) function makes a change to the HTTP headers (duh!). But in order for this to happen, no content must be been written to the HTTP body of the response before header() is invoked. Therefore you need to ensure nothing is written to the body - usually some extra whitespace before a <?php or in some included file is the reason for the error. Ensure that all <?php are right at the beginning of the file with no whitespace, and similarly for any included PHP files with the ?> which needs to be right at the very end (no trailing whitespace).
March 26, 200917 yr Using the header(...) function makes a change to the HTTP headers (duh!). But in order for this to happen, no content must be been written to the HTTP body of the response before header() is invoked. Therefore you need to ensure nothing is written to the body - usually some extra whitespace before a <?php or in some included file is the reason for the error. Ensure that all <?php are right at the beginning of the file with no whitespace, and similarly for any included PHP files with the ?> which needs to be right at the very end (no trailing whitespace). Also close your session start at the beginning <?php session_start();?> <?php // dBase file include "dbConfig.php";
Create an account or sign in to comment