Home > OS >  Fatal error: Uncaught Error: Call to undefined function header() - Beginner question
Fatal error: Uncaught Error: Call to undefined function header() - Beginner question

Time:11-16

My name is Thibaud, I'm beginner on PHP. I'm trying to set up a login logic to my "app" and encountering this error :

Fatal error: Uncaught Error: Call to undefined function header() in C:\xampp\htdocs\assignment11_autosession\login.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\assignment11_autosession\login.php on line 15

Here is my code :


session_start();
// If there is no parameter, error message is generated
if ( isset($_POST["who"])  && isset($_POST["pass"])) {
$pass = $_POST["pass"];
$salt = 'hello';
$stored_hash = 'hello';
$md5 = hash('md5', $salt.$pass);
    unset($_SESSION["who"]);  // Logout current user
    if (filter_var($_POST["who"], FILTER_VALIDATE_EMAIL) && $md5 == $stored_hash) {
            $_SESSION["who"] = $_POST["who"];
            $_SESSION["success"] = "Logged in.";
           ** header('Location: view.php');**
            return;
    }
    elseif (!filter_var($_POST["who"], FILTER_VALIDATE_EMAIL)) {
        $_SESSION["error"] = "Email must have an at-sign (@)";
        header('Location: login.php');
        return;}
    elseif ( strlen($_POST["who"]) < 1 || strlen($_POST["pass"]) < 1 ) {
    $_SESSION["error"] = "User name and password are required";
    header('Location: login.php');
    return;}
    else{ $_SESSION["error"] = "Incorrect password.";
            header( 'Location: login.php' );
            return;}
}
?>

<html>
<head>
<title>Thibaud  - login</title>
</head>
<body>
<h1>Thibaud - login page</h1>
<p>
<form method="POST">
<label for="nam">User Name</label>
<input type="text" name="who" id="nam"><br/>
<label for="id_1723">Password</label>
<input type="text" name="pass" id="id_1723"><br/>
<input type="submit" value="Log In">
<input type="submit" name="cancel" value="Cancel">

</form>
</p>

<p>
<?php
    if ( isset($_SESSION["error"]) ) {
        echo('<p style="color:red">'.$_SESSION["error"]."</p>\n");
        unset($_SESSION["error"]);
        unset($_SESSION["pass"]);
    }
    else {echo('<p style="color:green">'.$_SESSION["success"]."</p>\n");
        unset($_SESSION["success"]);
        unset($_SESSION["pass"]);
    }
?>
</p>
</body>
</html>

I tried to find out more about this error message on google. I understood it could be :

  • a syntax issue
  • a call to a function that has not been declared yet.

But I did not find signs of this in my code yet...

When I test to put a bad "who" or a bad "pass", browser redirection to the same page works well (http response 302 in the network console and refresh of the page). It's only in the case of a correct "who" and correct "pass" that the redirection to this other "view.php" page does not occur and I get this error message.

I tried to modify the syntax of this header() before and after, I tried to copy from another piece of code where it was working, but error remains there.

I hope this question is in the correct format and thanks a lot in advance for your help on this one :) Best Regards, Thibaud.

CodePudding user response:

After a further look at the code, I reworked it. This version of this code works well. I am not sure of the exact reason it's working but still, I let it here so that if a similar issue is coming for someone, he/she'll be able to retrieve a correct logic :

<?php

session_start();
// If there is no parameter, error message is generated
if ( isset($_POST["who"])  && isset($_POST["pass"])) {
$pass = $_POST["pass"];
$salt = 'XyZzy12*_';
$stored_hash = '1a52e17fa899cf40fb04cfc42e6352f1';
$md5 = hash('md5', $salt.$pass);
    unset($_SESSION["who"]);  // Logout current user
    if (filter_var($_POST["who"], FILTER_VALIDATE_EMAIL) && $md5 == $stored_hash) {
            $_SESSION["who"] = $_POST["who"];
            $_SESSION["success"] = "Logged in.";
            header('Location: view.php');
            return;}
    elseif (!filter_var($_POST["who"], FILTER_VALIDATE_EMAIL)) {
            $_SESSION["error"] = "Email must have an at-sign (@)";
            header('Location: login.php');
            return;}
    elseif ( strlen($_POST["who"]) < 1 || strlen($_POST["pass"]) < 1 ) {
            $_SESSION["error"] = "User name and password are required";
            header('Location: login.php');
            return;}
    else{ $_SESSION["error"] = "Incorrect password.";
            header( 'Location: login.php' );
            return;}
}
?>


<html>
<head>
<title>Thibaud LOYRIAC  - AUTOSDB - login</title>
</head>
<body>
<h1>Thibaud LOYRIAC  - AUTOSDB - login page</h1>
<p>
<form method="POST">
<label for="nam">User Name</label>
<input type="text" name="who" id="nam"><br/>
<label for="id_1723">Password</label>
<input type="text" name="pass" id="id_1723"><br/>
<input type="submit" value="Log In">
<input type="submit" name="cancel" value="Cancel">

</form>
</p>

<p>
<?php
    if ( isset($_SESSION["error"]) ) {
        echo('<p style="color:red">'.$_SESSION["error"]."</p>\n");
        unset($_SESSION["error"]);
        unset($_SESSION["pass"]);
    }
    elseif ( isset($_SESSION["success"]) ) {
    echo('<p style="color:green">'.$_SESSION["success"]."</p>\n");
        unset($_SESSION["success"]);
        unset($_SESSION["pass"]);
    }
?>
</p>
</body>
</html>

Thanks, Thibaud.

  • Related