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 :
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.
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.