I have the following problem and feel that the solution is simple but after 8 hours of trying and searching, I am giving up.
I have this simple page:
<?php
// Start the session
$lifetime=600;
session_set_cookie_params($lifetime);
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Change the Yoda!</title>
</head>
<body>
<?php
// Set session variables
$_SESSION["post-data"] = $_POST;
?>
<form action="yoda_is.php" method="POST">
YODA IS: <input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
Upon submit, it sends me to this page:
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Who is Yoda?</title>
</head>
<body>
<?php
// Echo session variables that were set on previous page
echo "YODA IS " . $_SESSION['post-data'] = $_POST['name'];
?>!
</body>
</html>
The value that you enter in the first page, is successfully being displayed on the second page.
However, once I close the browser window and revisit the second page, the value is no longer there and it returns an error.
My question is simple, what am I doing wrong / do I need to do in order for the value that I entered on the first page, to be there after I revisit the second page?
Thank you so much for your help and suggestions, in advanced.
KR
MD
On your first page remove this:
// Set session variables
$_SESSION["post-data"] = $_POST;
On your second page use this instead:
// If the user filled out the form, set our session variable to the new value
if(isset($_POST['name']))
{
$_SESSION['post-data'] = $_POST['name'];
}
// Echo session variable set above
echo "YODA IS " . $_SESSION['post-data'] . "!";
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Observation:
Sending form data from page_1 and page_2 does not show both data in result file.
If sending page_1 one sees the result in result file. When sending page_2 the result of page_1 is gone and you only see the page_2. It behaves as the content of $_SESSION['data'] is
overwritten every time one sends either page_1 or page_2.
The session id of all 3 pages are identical.
Wanted result:
Every time running result.php one should see the accumulated actions, e.g. if pressing page_1 first time you only see page_1 data, if pressing page_2 you would see both page_1- and page_2- data. When pressing page_1 the second time, the page_1 value should be updated and visible when pressing result.php
PHP version
Terminal: PHP 7.4.6
Browser: 7.3.18-1+ubuntu18.04.1+deb.sury.org+1
Page_1:
<?php
session_start();
echo session_id();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page-1</title>
</head>
<body>
<form method="post">
<br><br>
<input type="text" name="input_1" value="">
<button type="submit" name="button">Save</button>
</form>
<pre>
<?php
$_SESSION["data"] = $_POST;
print_r($_SESSION);
?>
</body>
</html>
Page_2:
<?php
session_start();
echo session_id();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page-2</title>
</head>
<body>
<form method="post">
<br><br>
<input type="text" name="input_2" value="">
<button type="submit" name="button">Save</button>
</form>
<pre>
<?php
$_SESSION["data"] = $_POST;
print_r($_SESSION);
?>
</body>
</html>
Result:
<pre>
<?php
session_start();
echo session_id() . "<br /><br />";
print_r($_SESSION);
Every time you send form you overwrite $_SESSION
You should use either
if(!empty($_POST)) {
$_SESSION["data"]['page1'] = $_POST; //and analogically for page2
}
OR
$_SESSION["data"] = array_merge($_SESSION["data"],$_POST);
It behaves as the content of $_SESSION['data'] is overwritten every time one sends either page_1 or page_2.
Because this is exactly what you're doing. The line $_SESSION["data"] = $_POST; appears in both page_1 and page_2, and there's nothing that preserves the old value of $_SESSION["data"] before new data is written.
If you want to add the data instead of replacing it, you could write, for example, $_SESSION["data"][] = $_POST; in both pages. This will create an array in $_SESSION["data"] and append $_POST data to it each time you open either page.
Don't know were is the mistake lies in the code.
// Code in Page1
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test1</title>
</head>
<body>
<FORM METHOD = "POST" action = "Page2.php">
<input type="text" name = "user">
<input type="submit" name = "submit" value = "Submit">
</FORM>
<?php
if (isset($_POST["submit"])){
$_SESSION['user'] = $_POST['user'];
}
?>
</body>
</html>
// Code in Page2
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test2</title>
</head>
<body>
<input type="text" name = "field3" value = <?php echo htmlspecialchars($_SESSION['user']); ?>>
</body>
</html>
I expect the input field-named as "field3" in Page2 to be filled with whatever value provided in field-named as "name" of Page1.
You are posting data to page2 when submitting the form
<FORM METHOD = "POST" action = "Page2.php">
The code which is inside the if statement never executed if (isset($_POST["submit"])){
You have to place if statement on Page2 at the top after session_start to make it work
if (isset($_POST["Submit"])){
$_SESSION['user'] = $_POST['user'];
}
The action page is page2 so you will never have a post action on the page 1 where you have html form. In case of you have POST action that leads to page 1 you still have issues on page2.
and On page2, change it as
value =" <?php echo htmlspecialchars($_SESSION['user']); ?>">
You are missing "
I've spent a lot of time today researching this site for my solution but I have had no luck. I'm currently trying to learn php and working on my second project. I can only use PHP. I originally had my delete session and redirect in a separate logout.php file. This was working but then I found out that I can't do this. I've been instructed that I need to "clear the login, delete the session, and redirect back to the login page" and do this within an isPostBack in the results.php file. After a lot of research today I thought I was understanding how to do this but I can't get it to work. Hoping I can get some help.
<?php
session_start();
//require_once('cookies.php');
$isPostBack = filter_input(INPUT_GET, 'submit');
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
// clear ALL session data from memory
// clean up the session and remove the session ID.
// redirect to index.php
endSession();
session_destroy();
header("Location: index.php");
} else {
// user did not click logout doNothing();
}
?>
<html lang="en">
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
<input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
<?php
foreach($_SESSION['answers'] as $answer){
echo "<p>$answer</p>";
}
?>
</section>
</body>
Try to provide name attribute
<input type="submit" id="submit" value="Logout" name="logout"/>
and use only logout variable in place of submit or provide two different fields
$isPostBack = filter_input(INPUT_GET, 'submit');
$isPostBack = filter_input(INPUT_GET, 'logout');
I seem to have found my solution. I needed to give the isPostBack variable a name that matched the name given to the logout button. I also needed to include !==NULL after the isPostBack. I changed endSession(); to $_SESSION = array(); According to my research, endSession(); "removes all session variables". It seems to be working as it should now. Here is my edited code.
<?php
session_start();
$isPostBack = filter_input(INPUT_GET, 'submit')!==NUll;
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
// clear ALL session data from memory
// clean up the session and remove the session ID.
// redirect to index.php
$_SESSION = array();
session_destroy();
header("Location: index.php");
} else {
// user did not click logout doNothing();
}
?>
<html lang="en">
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
<input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
<?php
foreach($_SESSION['answers'] as $answer){
echo "<p> $answer</p>";
}
?>
</section>
</body>
If you need to remove se particular session values you can use unset()
unset ($_SESSION['userid'])
Suppose, I have two pages: page01.php and page02.php (their code is presented below).
If I leave action attribute in the form on page01.php empty (i.e. action=""), then access page01.php, fill in the form, press submit, then access page02.php - it all works fine (i.e. $_SESSION variable stores the data submitted on page01.php and can be accessed and viewed on page02.php as expected).
However, when I try to make the form send the user to page02.php (by changing the action atrribute to action="page02.php") it looks like the $_SESSION global variable doesn't store data from page01.php.
My question is: does this happen because the user is redirected to page02.php immediately upon submission of form and the code between php tags on page01.php does not get executed?
I'm aware I can use $_GET or $_POST on page02.php to achieve the desired behavior, but I'm just trying to understand the way action attribute and $_SESSION interact. Thank you.
Page01.php:
<html>
<head>
<title>Page 01</title>
</head>
<body>
<h1>Please fill this form</h1>
<form action="" method="post">
Name: <input name="username">
<input type="submit" value="send">
</form>
<?php
session_start();
if(isset($_POST['username'])) {
$_SESSION['username']=$_POST['username'];
}
?>
</body>
Page02.php:
<html>
<head>
<title>Page 02</title>
</head>
<body>
<h1>Another temporary page is working</h1>
<?php
session_start();
$expectedName = "Bob";
if($_SESSION['username'] == $expectedName) {
echo "Welcome, Bob!";
}
else {
echo "Access denied. You are ". $_SESSION['username'] . ", not Bob.";
}
?>
</body>
action attribute is attribute you specify where the script that will need to be run after submitting.
so if you want user to be redirected after form has been submitted, you use header() function.
Hello i have my form and i want to implement this method for preventing double submitting on forms or going back
The simple code:
<?php
// start session
session_start();
// create unique token
$form_token = uniqid();
// commit token to session
$_SESSION['user_token'] = $form_token;
?>
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Prevent multiple POSTs </title>
</head>
<body>
<form action="submit.php" method="post">
<input type="text" name="bar" />
<input type="hidden" name="user_token" value="<?php echo $_SESSION['user_token']; ?>" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
And here the submit.php code:
<?php
session_start();
//We check if the token of the page and session match!
if($_POST['user_token'] == $_SESSION['user_token']) {
$message = 'Your download is Here ';
} else {
$message = 'Your request has expired, please go back and resubmit!';
}
echo "We say: " . $message;
// invalidate the token so it expires on view, important!
unset($_SESSION['user_token']);
?>
I uploaded this script to my server but whenever i click the submit button i get the same answer the We say: Your download is Here echo , why this i checked if my cookies are created and yes they exists as they should ,
i want that users that have submitted the form once , to cant do that another time until a refresh or setting a maximum time that these cookies can exists and than delete them so that the user after for eg 10 min can submit another form if he want so
Any help will greatly be welcomed. Thanks in advance. Updatet
session_start();
Add this in the begining of second script which recive your data from form.
More detailes in manual.
But the only way to recieve msg 'Your request has expired' you need to keep recieved data in some form on server (file or data base) I suppose.