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'];
}
?>`
Related
I want to write a code which would be remember name given by user and on next visit will welcome him with this given name. I don't really understand cookies and session yet so I would be thankful for any help. I wrote something like this:
File: 1.php
<?php
session_start();
?>
<html>
<form action="2.php" method="post">
Name:<input type="text" name="name"/></br>
<input type="submit" value="send"/>
</form>
</html>
<?php
$name=$_POST['name'];
setcookie('name',$name,time()+3600*24);
$_SESSION['name']=$name;
?>
File: 2.php
//2.php
<?php
session_start();
if(isset($_COOKIE['name']))
echo "Hello".$_SESSION['name'];
else
echo "Cookie doesnt exist";
?>
In the example below, 1.php is just used for submitting to 2.php so no PHP code is being used.
1.php
<html>
<form action="2.php" method="post">
Name:<input type="text" name="name"/><br>
<input type="submit" name="submit" value="send"/>
</form>
</html>
I've shown both instances of this below on how to set a cookie and session. You can refresh just 2.php and the cookie output should still show the value of $_COOKIE['name'].
2.php
<?php
session_start();
if (isset($_POST['name'])) {
$_SESSION['username'] = $_POST['name'];
setcookie('name',$_SESSION['username'],time()+3600*24);
}
//Session Value will show in first instance
echo "Session Name: " . $_SESSION['username'] . "<br>";
//Cookie Value will not how until you refresh page
echo "Cookie Name: " . $_COOKIE['name'] . "<br>";
?>
Edit: Variables will not be overwritten when the page is refreshed.
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;
}
?>
I am trying to display session information like username, as user login through login page, the session has to capture user entered username and should display in page. Below i have tried php script, but its not echoing the username, Kindly check in the script for errors, thanks in advance.
<?php
session_start();
$_SESSION['test']= $_POST['myusername'];
$name= $_SESSION['test'];
echo $name;
?>
<form action="login.php" method="post">
<p>Username</p>
<input name="myusername" type="text" id="myusername" required>
<p>Password</p>
<input name="mypassword" type="password" id="mypassword"required></br>
<button><img src="http://icons.iconarchive.com/icons/webiconset/application/32/Register-icon.png" /></button>
</form>
login.php
Output i am getting is , simply its going to next page without displaying user name.
You can't access session data until after you call session_start(). So your first if statement is unnecessary and problematic as you can't check if a session variable exists until after you start your session. Also, make sure session_start() is called at the top of every page you wish to use sessions.
<?php
session_start();
$_SESSION['test']= $_POST['myusername'];
You must varify first that is session started or not. you can check it by using this code for Version PHP >= 5.4.0:-
if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
or
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
or by this code for Version PHP < 5.4.0:-
if (session_id() === "") { session_start(); }
Then you can see all session stored values just by printing them as array.
echo "<pre>";
print_r($_SESSION);
then you can assign to session your post varible value like this.
$_SESSION['test']= $_POST['myusername'];
echo $_SESSION['test'];
You are setting session before post. Please use below code.
login.php
<?php
if(isset($_POST['myusername']))
{
// your code
session_start();
$_SESSION['test']= $_POST['myusername'];
}
?>
<form action="login.php" method="post">
<p>Username</p>
<input name="myusername" type="text" id="myusername" required>
<p>Password</p>
<input name="mypassword" type="password" id="mypassword"required></br>
<button><img src="http://icons.iconarchive.com/icons/webiconset/application/32/Register-icon.png" /></button>
</form>
newpage.php
<?php
session_start();
echo $_SESSION['test'];
?>
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');
}
?>
Username and password not appear on Page 2.PHP although I post it to Page2.PHP
Page1.PHP
<form name="form1" method="post" action="Page2.php">
<input type="text" name="txtLogin">
<input type="password" name="txtPWD">
<input type="submit" name="btnSub" value="go">
</form>
Page2.PHP
<?php
if(isset($_REQUEST['txtLogin']))
{
session_start();
$_SESSION['login']=$login;
}
if(isset($_SESSION['login']))
header('Location: detail.php');
else
header('Location: index.html');
?>
put this on page2.php
if(isset($_POST['txtLogin']) && isset($_POST['txtPWD']))
{
//get values & do other scripts like saving values on sessions
$user = $_POST['txtLogin'];
$pass = $_POST['txtPWD'];
echo $user.'<br>'.$pass;
}
else
{
//event here
}
The problem is here:
$_SESSION['login']=$login;
You are using the $login variable, but it isn't actually being set anywhere.
A few lines further up, we see that the login name is actually in $_REQUEST['txtLogin'], not $login. So you should be using that.
$_SESSION['login']=$_REQUEST['txtLogin'];
Hope that helps.
Check settings: enable_post_data_reading, request_order, variables_order, gpc_order on http://www.php.net/manual/en/ini.core.php