Header() not redirecting

Codes here !

Moderators: egami, macek, gesf

Post Reply
bob101plod
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 10, 2020 11:18 am

Fri Mar 20, 2020 10:10 am

Hi,
I posted this as a follow-on to a different topic but that never had any views so I thought I'd best start a new topic -

I have the following code as my login.php

Code: Select all

require 'connect.php';
//reads the database connect details and connects to the server

//If the POST var "login" exists (from the submit button), then we can
//assume that the user has submitted the login form to run this script.
if(isset($_POST['login'])){
    
    //Retrieve the field values from our login form.
    $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
    $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
    
    //Retrieve the user account information for the given username.
    $sql = "SELECT id, username, password FROM adminusers WHERE username = :username";
    $stmt = $pdo->prepare($sql);
    
    //Bind value.
    $stmt->bindValue(':username', $username);
    
    //Execute.
    $stmt->execute();
    
    //Fetch row.
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    //If $row is FALSE.
    if($user === false){
        //Cannot find a user with that username!
               die('Incorrect username / password combination!');
    } else{
        //User account found. Check to see if the given password matches the
        //password hash that was stored in the adminusers table.
        
        //Compare the passwords.
        $validPassword = password_verify($passwordAttempt, $user['password']);
        
        //If $validPassword is TRUE, the login has been successful.
        if($validPassword){
            
            //Provide the user with a login session and Redirect to our protected page, which is called welcome.php
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['logged_in'] = time(); 
            header('Location: welcome.php');
            /* Make sure that code below does not get executed when it redirects. */
            exit;
            
        } else{
            //$validPassword was FALSE. Passwords do not match.
            die('Incorrect username / password combination!');
        }
    }
    
}
and the following is the login.html

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
    </head>
    <body>
        <h1>Login</h1>
        <form action="login.php" method="post">
            <label for="username">Username</label>
            <input type="text" id="username" name="username"><br>
            <label for="password">Password</label>
            <input type="text" id="password" name="password"><br>
            <input type="submit" name="login" value="Login">
        </form>
    </body>
</html>
Although the login details are correct, and no error message is passed, the page is not redirecting to welcome.php but, for some bizzare reason is going to a totally different php file in the same directory. The connect.php echos that the connection has been made succesfully and my file - register.php - connects and correctly adds a username and hashed password to the adminusers table.

If anyone has any ideas why, and how I can make it go to the correct redirected file, I would be very grateful (sorry, not financially as I'm doing this for a charity so I'm not getting anything for it either) :)
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1192
Joined: Mon Feb 22, 2016 5:52 pm

Fri Mar 20, 2020 12:42 pm

What is the name of the different page that it goes to?

In you script, you have provided header() with a relative path 'welcome.php' which will open the file in the same folder, if you want to direct to the web site root, then you should have used a slash '/welcome.php'

Incidentally, you had 104 views (as of this post) and 2 replies to your last post
bob101plod
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 10, 2020 11:18 am

Sat Mar 21, 2020 3:10 am

Thanks Hyper.

Unfortunately I added this problem to a topic that I had already posted - about a different problem. That's probably why there was no direct response to this particular question.

welcome.php is in the same directory as the login file , that is why I assumed that it would make the call directly to it. I've looked at other forums about the same question and have checked my file against several of the suggestions eg there was a command such as ECHO before the line with the header() command or there was a prompt or something after it which was causing the browser to hang, but I cannot see where I'm going wrong.

I've checked the pages of w3schools.com, php.net etc but could not find a solution. I can't work out why the page is redirecting to login3.php (one of several variants of login.php I've tried to solve the problem).

Thanks in advance
bob101plod
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Mar 10, 2020 11:18 am

Sat Mar 21, 2020 5:23 am

I am a DUMBASS and I apologise profusely. The error was not in the coding above but in the welcome.php coding of the $session variable being passed. As the file could not find the apropriate variable it dumped me to the page I had for an error login attempt.

The saying - read, read, read again is so true.
Post Reply