posting variable from php file to another php file - php

I show similar threads, but could not get clear through them.
page1.php
<?php
$id = 1234;
//Post $id to page2.php
?>
page2.php
<?php
$user_id=$_POST['id']; //should receive id posted from page1.php
?>

Actually you are not sending the id parameter to your Page2.php
Page1.php
<?php
$id = 1234;
header("location:page2.php?id=$id");//Post $id to page2.php
?>
Page2.php
<?php
echo $user_id=$_GET['id']; //should receive id posted from page1.php
?>

You can use sessions for this also, to show you what your options are.
This works with a POST method (use all in one file for the form method)
Form method (page1.php)
<?php
session_start();
$id = $_SESSION["id"] = $_POST['id'];
if(isset($_POST["submit"])){
echo $id;
echo "<br>";
echo "<a href='page2.php'>Click to see session ID on next page</a>";
}
?>
<form action="" method="post">
Enter ID:
<input type="text" name="id">
<br>
<input type="submit" name="submit" value="Submit">
</form>
page2.php
<?php
session_start();
$user_id=$_SESSION["id"];
echo $user_id; // will echo 1234 if you entered "1234" in the previous page.
Regular session method (page1.php)
<?php
session_start();
$id = $_SESSION["id"] = "1234";
echo $id; // will echo 1234
page2.php
<?php
session_start();
$user_id=$_SESSION["id"];
echo $user_id; // will echo 1234
Footnotes:
You could then use the same (session) variable for a member's login area, database for example.
It is important to keep session_start(); included inside all pages using sessions, and at the top.
Should you be using a header() in conjunction with this, then you will need to add ob_start(); before session_start();
Otherwise (as Eitan stated in a comment) "$_SESSION value will be unresolvable."
header() example:
<?php
ob_start();
session_start();
$user_id=$_SESSION["id"];
if(isset($_SESSION["id"])){
header("Location: members_page.php");
exit;
}
else{
header("Location: login_page.php");
exit;
}
You could also replace: if(isset($_SESSION["id"])) with if(!empty($_SESSION["id"]))
To implement a logout page, you would need to use session_destroy();

Related

Echoing a name from session php

I'm a student and I'm making a quiz using php and mysql, my problem is I'm trying to echo a name on the results page but it doesn't work.
My first page is an index page where I create a form which gets the users name which I send to my quiz.php page.
<form method="post" action="quiz.php">
<img src="pictures/indeximage.jpg" alt="horrormovies" width="1024" height="640">
<p>
Please Enter Your Name
<br>
<input type="text" name="name">
</p>
<input type="submit" name="submit" value="Start">
</form>
on my quiz.php page i put make a variable and put it in a session
<?php
//start session
session_start();
$var_name=$_REQUEST['name'];
$_SESSION['ses_name']=$var_name;
?>
On my results page I have this
<?php
session_start();
$var_name=$_SESSION['ses_name'];
?>
<p>
Thank you for taking the quiz <?php echo $var_name; ?>.
</p>
Use isset for assign value in session variable. for good practice.
if(isset($_POST['submit']))
{
//start session
session_start();
$var_name=$_REQUEST['name'];
$_SESSION['ses_name']=$var_name;
}
quiz.php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['ses_name']=$_REQUEST['name'];
}
Try this code :-
results page
<?php
//start session
session_start();
if(!empty($_SESSION['ses_name']))
{
?>
<p>Thank you for taking the quiz <?php echo $_SESSION['ses_name']; ?>.</p>
<?php
}
else{
echo 'session not set ';die;
}
?>

Checking the session value that is available or not

I wrote the following login.php file.
<?php
session_start();
//Check everything and if everything is correct and the username and password is correct and available
echo "Successfully";
$_SESSION['login_user'] = $username;
// and etc
?>
Now if the username is session as the the result $_SESSION['login_user'] value is session also.
and then I create check-session.html file and it is as follows:
<html>
<body>
<form method = "POST" action = "check.php">
<input type = "submit" value = "check-session">
</form>
</body>
</html>
And then the check.php file is as follows:
<?php
if(isset($_SESSION['login_user'])) {
echo "session is available";}
else { echo "session is not available"; }
?>
But the problem is when the login operation is successfully and now I want to know that the session is created really or not, after clicking the check-session button in the check-session.html page, I see the result from server as the follows:
session is not available
Also for more information I use wamp server.
Put session_start(); in the start of every page that's using sessions or is related to them in any way.
In the start of your check.php file
<?php
session_start();
if(isset($_SESSION['login_user'])) {
echo "session is available";}
else { echo "session is not available"; }
?>
You can solve this problem by making a separate file for setting session and include that file on the starting of each logged in page.
this is c.php for checking session...
<?php
include 'b.php';
if(isset($_POST['check_session']))
{
if(isset($_SESSION['login_user']))
echo "session is available";
else
echo "session is not available";
}
?>
<form method = "POST" action = "c.php">
<input type = "submit" name="check_session" value = "check-session">
</form>
a.php for login
<?php
if(isset($_POST['login']))
{
header("Location: c.php");
}
?>
<html>
<body>
<form method = "POST" action = "a.php">
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
and also make b.php simply for setting session using session_start()

PHP values not retrieving from other page

I have this page I am working on which is a sort of a game.
From page to page I am passing the user name like this
index:
<form method="GET" action="start.php">
<label> Name: </label>
<input type="text" name="username"><br>
<input type="submit">
</form>
Where I retrieve the name on the second page like this:
<?php
session_start();
$_SESSION['username'] = $_GET['username'];
?>
-
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Welcome: " . $_SESSION['username'];
}
else{
echo "Name is unknown";
}
?>
And also on the third:
<p>
<?php
session_start();
echo $_SESSION['username']
?>
</p>
And this code is working just fine. Now I was making an if statement which, when ever you don't enter a name, you won't continue to the next page. I added this code to the first page and this is working for the first page.
<?php
session_start();
$_SESSION['username'] = $_GET['username'];
if($_SESSION['username'] != "")
{
header("Location: start.php");
}
?>
So after adding this, you won't go further unless you do enter a name. But by doing this for a reason I don't know yet and couldn't find, the $_SESSION['username'] = $_GET['username']; isn't working and the names are not passed through
This is the link if you like to play: http://i333180.iris.fhict.nl/site
(not finished yet)
These are your problems:
1- You don't need to use more than one session_start() in a file.
2- you have closed php tag in the second code and then countinued the PHP coding.
3- before session_start(), it must not send any header or echo anything. In the 3rd code, you have echoed tag before session_start().
4- same as session_start(), you must not send any header or echo anything before calling header function. Be sure in the 4th page, you do not have anything echoed before header().

PHP session variable not being set

I am aware that there are several topics about this but after hours of reading I still can't figure out what's going wrong.
I'm building a survey with a login screen at index.php. The user is required to insert their name and submit the form. The username should be saved and passed onto setup1.php.
This is part of my index.php:
<?php
session_start();
print session_id();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>
Start of setup1.php:
<?php
session_start();
print session_id();
print_r($_SESSION);
echo $_SESSION['username'];
?>
My $_SESSION variable is empty, I have nothing printed on the following page setup.php. I would appreciate if you could help.
Your $_POST code is in the wrong file. Your form is going to setup1.php, but you're trying to set the $_SESSION in your index.php.
You need to take it out of there and put it in setup1.php:
<?php
session_start();
if (!isset($_POST['participant'])) {
die('No $_POST data');
}
$_SESSION['username'] = $_POST['participant'];
print session_id();
print_r($_SESSION);
echo $_SESSION['username'];
?>
Also, make sure that you're using $_SESSION and not %_SESSION. I hope it was just a typo.
Your form hasn't been submitted when you set the $_SESSION['username'], i.e., $_POST['participant'] has no value.
You should move the piece of code below from index.php to setup1.php
<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>
index.php
<?php
session_start();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
setup1.php
<?php
session_start();
if(isset($_POST['participant']) && ! empty($_POST['participant']))
{
$_SESSION['username'] = $_POST['participant'];
echo $_SESSION['username'];
}
?>`

Session variable gets lost between pages

I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:
account.php
<form action="afterlog.php" class="form" method="post">
<input type="text" name="amid" id = "amid" class="input" />
<input class="btn" type="submit" value="Go" />
</form>
which posts the value 'amid' to afterlog.php
afterlog.php
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
header('Location: customer_view.php');
}
?>
which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php
customer_view.php
I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:
<?php
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>
Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.
What I am doing wrong?
Thanks for your help!
You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
or simply:
$_SESSION['account_manager_id'] = $_POST['amid'];
in afterlog.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$account_manager_id = $_POST['amid'];
$_SESSION['account_manager_id']=$account_manager_id;
header('Location: customer_view.php');
}
?>
or
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$_POST['amid'];
header('Location: customer_view.php');
}
?>

Categories