How to redirct user to welcome page when login

am new in php am trying to make a redirect link like when a user login successfully it will redirect he/she to login page. each time i login it will show me a blank page this is my code.

<!DOCTYPE HTML>
<?php
session_start(); // Starting Session

if (isset($_POST['submit'])) {  
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $username = "xxxxx";
    $password = "xxxxxx";
    $database = "xxxxxx";
    $server = "xxxxxxx";

    $connection = mysql_connect("$server", "$username", "") or die("can not connect to server");
    // To protect MySQL injection for Security purpose
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    // Selecting Database
    $db = mysql_select_db("$database", $connection);
    // SQL query to fetch information of registerd users and finds user match.
    $query = mysql_query("select * from sec where password='".$password."' AND username='".$username."'", $connection);
    $result = mysql_num_rows($query) or die(mysql_error());
    if ($result == 1){
        $_SESSION['username'] = $username; 
        header("Location: welcome.php");
    }else{
        echo "invalid user credentials";
    }
}

?>

Ok first things first.

You shouldn’t be using the mysql functions anymore as they are deprecated. You should be using mysqli or pdo ( I would go with pdo ).

Now then your first line of code is this <!DOCTYPE HTML> As soon as the request hits the page, that html code will be send to the browser along with the header. Then when you do the header("Location: welcome.php"); it can’t send the header again. This will cause an PHP Notice that depending on your server settings might get displayed on the screen or not. If not then you will find it in your error_log. As it can’t redirect, it will just continue processing the file and as there is no further output you get a white page.

So to fix it, delete the html code. Also to make sure the php script stops processing, add an exit(); after your header() function.

And finally, you can remove the final ?> this will reduce the risk of whitespace errors at a later point.

Hope this helps.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.