How Can I Make This 2-Page PHP Login Script Remember User Input On Page 2

I'm struggling to create a two-paged form using $_SESSION. What I want to achieve is the first page (page1.php) requires the user to enter his/her email address. And the second page (page2.php) requires the user to enter his/her password.
When the user submits page1.php, it takes you to page2.php, where the email address submitted will be printed. But unfortunately, the email address is not printed, as intended.
Please, note I've tried to adopt related resolved threads, but I'm still missing something.
<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>2 Step Login - Page 1</title>
<link href="page1.css" rel="stylesheet">
</head>
<body>
<div id="formwrap">
<div id="form_inner">
<div id="logo">
<img src="" alt="logo">
</div>
<div id="email">
</div>
<div id="pwd">
Sign in
</div>
<div id="form">
<form action="page2.php" method="post" enctype="multipart/form-data" name="form">
<?php
//On page 1
$_SESSION['username'] = $var_value;
?>
<input id="username" name="username" type="text" placeholder="Email" autofocus required>
<div id="forgot">No Yet A Member, Register Here</a></div>
<input type="hidden" name="username" value="var_value">
<input id="send" name="submit" type="submit" value="Next">
</form>
</div>
</div>
</div>
</body>
</html>
Answer
Solution:
From your code, it looks like you've skipped the step from using the POST value from the first form before getting to the second.
After a form is submitted, in PHP a $_POST variable contains the values of what was in the form from the keys of the name in the form.
For example<input name="myinput">
in a form set to POST will result in$_POST['myinput']
containing the value from the submit you submit.
The simple way to achieve what you want would be to just post the value frompage1.php
topage2.php
, and then it looks like you handle the login on alogin.php
. For example:
page1.php
<?php
if (isset($_GET['invalidEmail'])) {
echo "Error: Invalid email address entered.";
}
?>
<form action="page2.php" method="post">
<input type="email" type="email" />
<input type="submit" value="Continue" />
</form>
page2.php
<?php
$email = htmlentities($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// the email address is not valid, redirect them back to page1.php
header('Location: page1.php?invalidEmail=true');
}
?>
<form action="login.php" method="post">
<input type="hidden" value="<?=$email?>" />
<input type="password" type="password" />
<input type="submit" value="Continue" />
</form>
login.php
<?php
session_start(); // before any HTML is echoed
$email = $_POST['email'];
$password = $_POST['password'];
// ..do whatever you need todo to validate the email and password
// now you can use something like $_SESSION['loggedin'] = $email which will persist across any pages
// you can check `if (isset($_SESSION['loggedin']))` to check if they're signed in
// if the validation fails, redirect them back to page1.php with another error
?>
This way you're not using$_SESSION
between pages and a user must go through from entering their email on page 1, to then adding their password on page 2 and finally validating on thelogin.php
file.
The above will get the basics right for you and hopefully allow you to get where you need to go. There are a few extra things you will want to consider (though without having a firm understanding of the above, the rest won't work..)
- Validation between pages - from page1 (collecting email) to page2, I've added a line to collect and validate an email address format. You might want to do some extra validation to see if it exists in your records before asking for a password.
- Input sanitization - you'll notice I've used
htmlentities
around the$_POST['email']
- this is to prevent users from injecting extra code on page2 - From page2.php, if you visit it directly you're always redirected to page1.php because it requires an email address to be posted to it. You may want to consider what should happen here.
- The flow here also means you can't skip between pages and must go through page1 => page2 => login. This is almost to be expected, but setting
$_SESSION['email'] = $_POST['email']
would allow you to persist the entered data between pages.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
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/
CSS
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML
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 programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
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.