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