Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

php session variable is not passed

9 views
Skip to first unread message

.

unread,
Dec 28, 2016, 8:39:15 PM12/28/16
to
I am facing problem with php sessions . Suppose I have two pages 1)form.php 2)receiver.php . I want to pass a variable frrom form.php to receiver.php . here is my code




form.php :


<?php
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body style="color: white ; background-color: #2aabd2 ; ">
<br>
<form method="post" enctype="multipart/form-data" action="receiver.php">
<label for="userId">USERNAME : </label>
<input type="text" placeholder="Enter username" id="userId" name="uF"
required>
<button type="submit" name="sb">SUBMIT</button>
<button type="reset" name="rb">Reset</button>
</form>
<br>

<?php
if (isset($_POST["sb"]))
{
$u = $_POST["uF"];
echo "u : " . $u . "<br>";
$_SESSION["username"] = $_POST["uF"];


echo "get : " . "<br>";
print_r($_GET);
echo "<br>";
echo "post : " . "<br>";
print_r($_POST);
echo "<br>";
echo "session : " . "<br>";
print_r($_SESSION);
echo "<br>";

}
?>
</body>
</html>



receiver.php :

<?php
session_start();


$r =$_SESSION["username"];
echo "r : "."<br>";
echo $r;
echo "<br>";

echo "get : "."<br>";
print_r($_GET);
echo "<br>";
echo "post : "."<br>";
print_r($_POST);
echo "<br>";
echo "session : "."<br>";
print_r($_SESSION);
echo "<br>";


?>




My goal is to pass a variable from form.php to receiver.php and print it .
but in receiver.php i could find (print) the variable that i passed .

Ben Bacarisse

unread,
Dec 28, 2016, 9:20:28 PM12/28/16
to
"." <wavesof...@gmail.com> writes:

> I am facing problem with php sessions . Suppose I have two pages
> 1)form.php 2)receiver.php . I want to pass a variable frrom form.php
> to receiver.php . here is my code
>
>
> form.php :
>
>
> <?php
> session_start();
> ?>
>
> <!DOCTYPE html>
> <html lang="en">
> <head>
> </head>
> <body style="color: white ; background-color: #2aabd2 ; ">
> <br>
> <form method="post" enctype="multipart/form-data" action="receiver.php">
> <label for="userId">USERNAME : </label>
> <input type="text" placeholder="Enter username" id="userId" name="uF"
> required>
> <button type="submit" name="sb">SUBMIT</button>
> <button type="reset" name="rb">Reset</button>
> </form>
> <br>
>
> <?php
> if (isset($_POST["sb"]))

This is very unlikely to be true. This "page" is likely going to be the
result of a GET request rather than a POST.

> {
> $u = $_POST["uF"];
> echo "u : " . $u . "<br>";
> $_SESSION["username"] = $_POST["uF"];
>
>
> echo "get : " . "<br>";
> print_r($_GET);
> echo "<br>";
> echo "post : " . "<br>";
> print_r($_POST);
> echo "<br>";
> echo "session : " . "<br>";
> print_r($_SESSION);
> echo "<br>";
>
> }
> ?>
> </body>
> </html>

>
> receiver.php :
>
> <?php
> session_start();

But this code is the result of a POST request -- specifically the result
of submitting the form shown above. In other words it's here where you
need to look at $_POST["uF"]. If you need to set a session variable,
it's here you would do it.

> $r =$_SESSION["username"];
> echo "r : "."<br>";
> echo $r;
> echo "<br>";
>
> echo "get : "."<br>";
> print_r($_GET);
> echo "<br>";
> echo "post : "."<br>";
> print_r($_POST);
> echo "<br>";
> echo "session : "."<br>";
> print_r($_SESSION);
> echo "<br>";
>
>
> ?>
>
>
> My goal is to pass a variable from form.php to receiver.php and print
> it .

That happens as a result of submitting the form. You don't need to do
anything with session variables at all.

--
Ben.

Jerry Stuckle

unread,
Dec 28, 2016, 10:36:15 PM12/28/16
to
Your problem is in your understanding on how PHP and the web works.

ALL of the code in form.php will be executed before the page is sent to
the client. This includes the HTML for the form - but also the PHP code
following. Processing the values in the form can only be done after the
form is submitted, which in your case would be "receiver.php".

There are two common ways to handle this. One is to just place the PHP
code in receiver.php. Process the input, and if correct continue. If
not correct, redirect back to form.php.

he other is to place the PHP code at the top of form.php and change your
form to post back to the form.php. If you get the values in the $_POST
array, redirect to receiver.php. If not, display the form.

The thing to remember is that web pages are a bit different from most
other programming. Web pages are transactional - which means the client
makes a request to the server, the server processes that request and
sends the results to the client. The server process (not to be confused
with an OS process) then terminates and waits for another request. Each
request from the client starts a new process on the server.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

J.O. Aho

unread,
Dec 29, 2016, 4:30:51 AM12/29/16
to
On 29.12.2016 02:39, . wrote:
> I am facing problem with php sessions . Suppose I have two pages 1)form.php 2)receiver.php . I want to pass a variable frrom form.php to receiver.php . here is my code
>
>
>
>
> form.php :
>
>
> <?php
> session_start();
> ?>
>
> <!DOCTYPE html>
> <html lang="en">
> <head>
> </head>
> <body style="color: white ; background-color: #2aabd2 ; ">
> <br>
> <form method="post" enctype="multipart/form-data" action="receiver.php">

You are telling that the page to where you post is receiver.php, so the ...

> <label for="userId">USERNAME : </label>
> <input type="text" placeholder="Enter username" id="userId" name="uF"
> required>
> <button type="submit" name="sb">SUBMIT</button>
> <button type="reset" name="rb">Reset</button>
> </form>
> <br>
>
> <?php
> if (isset($_POST["sb"]))
> {

code here is never executed ...

> $u = $_POST["uF"];
> echo "u : " . $u . "<br>";
> $_SESSION["username"] = $_POST["uF"];
>
>
> echo "get : " . "<br>";
> print_r($_GET);
> echo "<br>";
> echo "post : " . "<br>";
> print_r($_POST);
> echo "<br>";
> echo "session : " . "<br>";
> print_r($_SESSION);
> echo "<br>";
>
> }
> ?>
> </body>
> </html>
>
>
>
> receiver.php :
>
> <?php
> session_start();
>
>
> $r =$_SESSION["username"];

so the session value username never been set

> echo "r : "."<br>";
> echo $r;
> echo "<br>";
>
> echo "get : "."<br>";
> print_r($_GET);
> echo "<br>";
> echo "post : "."<br>";
> print_r($_POST);
> echo "<br>";
> echo "session : "."<br>";
> print_r($_SESSION);
> echo "<br>";
>
>
> ?>

There are two ways you can solve this, the simple way, just remove the
PHP code in your form page
--- form.php ---
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body style="color: white ; background-color: #2aabd2 ; ">
<br>
<form method="post" enctype="multipart/form-data" action="receiver.php">
<label for="userId">USERNAME : </label>
<input type="text" placeholder="Enter username" id="userId"
name="uF"
required>
<button type="submit" name="sb">SUBMIT</button>
<button type="reset" name="rb">Reset</button>
</form>
<br>
</body>
</html>
--- EOF ---

and in your receiver script you set the post data

--- receiver.php ---
<?php
session_start();

if (isset($_POST["sb"]) && !empty($_POST["uF"];)) {

$u = $_POST["uF"];
echo "u : " . $u . "<br>";
$_SESSION["username"] = $_POST["uF"];


echo "get : " . "<br>\n";
print_r($_GET);
echo "<br>\n";
echo "post : " . "<br>\n";
print_r($_POST);
echo "<br>\n";
echo "session : " . "<br>\n";
print_r($_SESSION);
echo "<br>\n";
echo "Please visit our <a href='thirdpage.php'>welcome page</a>.<br>\n";
} else {
echo "Nothing was posted that we can use<br>\n";
}

?>
--- EOF ---

This is how things are traditionally done. When you have set the session
data, then you can go to a third page and the script could look like
this (keep in mind there are no error handling, than you should fix
yourself)

--- thirdpage.php ---
<?php
session_start();

echo "Hello ".$_SESSION["username"]."!!<br>\n";
?>
--- EOF ---



The second way you could do this, but require you use buffering (no code
this time), you would need to change the post of the form.php to be to
form.php instead of receiver.php.
After you set the session variable username you use header() to redirect
to receiver.php.
If you don't buffer your output, then there will be data sent before the
header is sent, which will make the header() to fail and as it has great
potential to fail for you, I don't provide any example.


--

//Aho

Jerry Stuckle

unread,
Dec 29, 2016, 9:51:36 AM12/29/16
to
On 12/29/2016 4:30 AM, J.O. Aho wrote:
>
>
> The second way you could do this, but require you use buffering (no code
> this time), you would need to change the post of the form.php to be to
> form.php instead of receiver.php.
> After you set the session variable username you use header() to redirect
> to receiver.php.
> If you don't buffer your output, then there will be data sent before the
> header is sent, which will make the header() to fail and as it has great
> potential to fail for you, I don't provide any example.
>
>

Buffering is unnecessary. Just place the PHP code in form.php ahead of
the html. If the $_POST array is set, redirect the page. If not, drop
down to the html form.

This is actually the preferred way, because if you have multiple fields
in the form you can validate each of the fields. If all are correct,
redirect. If one or more is not correct, you can redisplay the form
with the previously entered values filled in an an error message
indicating the error(s). Very difficult if you post to a different page.

And once again you don't need to buffer the output.
0 new messages