PHP redirection

hi i have been making a registration and login system i have encountered a problem when running the script on my web server. im getting this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/commoncr/public_html/registeration/login.php:9) in /home/commoncr/public_html/registeration/login.php on line 25

i’m not sure what is happening it will not re direct me to the index.php.
bellow is the code im using:
[php]

<?php require('db.php'); // If form submitted, insert values into the database. if (isset($_POST['username'])){ $username = $_POST['username']; $password = $_POST['password']; $username = stripslashes($username); $username = mysql_real_escape_string($username); $password = stripslashes($password); $password = mysql_real_escape_string($password); //Checking is user existing in the database or not $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'"; $result = mysql_query($query) or die(mysql_error()); $rows = mysql_num_rows($result); if($rows==1){ $_SESSION['username'] = $username; header("Location:index.php"); // Redirect user to index.php }else{ echo "

Username/password is incorrect.


Click here to Login
"; } }else{ ?>

[/php]

Your code has more security holes it makes swiss cheese look like a brick wall. ;D

  1. mysql is obsolete and you should be using mysqli or PDO (my recommendation) - goto php.net for more info
  2. you should be using prepared statements
  3. you should use password_hash http://php.net/manual/en/function.password-hash.php instead of md5

The answer to you question is you are passing headers before the rest of the php script finishes passing headers (code output).

[php]header(“Location: index.php”):
exit(); // You really should put this in also[/php]

The easiest way to fix it for people learning php (at least for me it was) is that the above has to be the last statement the php script executes period.

You are using deprecated code that will not work at all in the current version of Php. You need to be using PDO with prepared statements.

Sponsor our Newsletter | Privacy Policy | Terms of Service