Storing form data in session with php - php

I'm testing a really basic PHP form, where the form data is saved in a session.
Later, i want that session data to be the default value of the form:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="var" value=<?php $name ?>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
}
echo $name;
?>
So, for example if i input "MyName" it should echo "MyName" and in the form there should be the value "MyName". The problem with the actual code is that it gives an E_NOTICE : type 8 -- Undefined variable: name -- at line 18 error. I think that the variable is not being stored, can someone help me out on this?

The first error I notice is this piece of code:
<form action="" method"post">
Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>
<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>

Spotted a few things:
I'd put the session_start(); at the top of the page before outputting anything.
Your method was incorrectly written it needs a '=' when specifying the method - thats the main reason why nothing was being stored, the form wasn't submitting properly.
Same with how you've put in the value on the name input - it has no '=' and you don't close the input tag properly - I've left it blank and added a placeholder - you can change it to what you need.
Heres how I'd do it:
<?php session_start(); ?>
<strong>Test Form</strong>
<form action="" method="post">
<input type="text" name="var" value="" placeholder="enter name">
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
}
// Store the session in a variable after the submit - otherwise it will be forgotten on refresh
$name = $_SESSION['var'];
// check if session exists
if(isset($name)) {
echo $name;
}
else {
echo 'no name entered...';
}
?>
You can edit the above to hide the form if a name has been submitted etc. Use session_destroy(); to reset the stored session.
Cheers

Related

Session lost from php page to codeignighter project

I have a button that POST data to my site
<form action="https://www.mysite.co.uk/some/index.php" method="POST">
<input type="hidden" name="userid" value="ZUpmenVaN0ZVTTBmejNGZGNwZGFha1NmR0tuSjdaT3VYdjV5cTF4WGtISzRvK0ptOC9vZmQyc3J3T3cwTmplbWZ3alhod0xMYUhlQ2xLSng4WWI4ZEE9PQ2">
<input type="submit" value="Go" style="font-size:14px; padding:20px;">
</form>
I then turn it into usable session
mysite.co.uk/some/index.php
<?php session_start();
<?php
if (isset($_POST['userid'])) {
$_SESSION['userid'] = $_POST['userid'];
$userid = $_SESSION['userid'];
}
if (isset($_SESSION['userid'])) {
echo "You are logged in and have access to these tests.";?>
code here
<?php
} else {
header ('location: ../index.htm');
}
?>
this all works well.
I now want to use this within a countersign project with the same website (this is where it goes wrong)
mysite.co.uk/some/tests/ETray/New/
<?php
$this->load->library('session');
$this->session->set_userdata('user_id', $_POST['userid']);
$userID = $this->session->userdata('user_id');
echo "$userID";
?>
I get error
Message: Undefined index: userid
unless I post direct to this page like
<form action="https://www.mysite.co.uk/some/tests/ETray/New/" method="POST">
<input type="hidden" name="userid" value="ZUpmenVaN0ZVTTBmejNGZGNwZGFha1NmR0tuSjdaT3VYdjV5cTF4WGtISzRvK0ptOC9vZmQyc3J3T3cwTmplbWZ3alhod0xMYUhlQ2xLSng4WWI4ZEE9PQ2">
<input type="submit" value="Go" style="font-size:14px; padding:20px;">
</form>
Then no error and the echo prints out.
Question
how do I get this to echo out on this page with the first form not the second ?
Looks like that error Message: Undefined index: useridis due to the undefined index of $_POST['userid']
Another one, where does mysite.co.uk/some/tests/ETray/New/ points to?? Is that inside controller or where???

Form Text to specify session name

I want to allow the user to input some text, then use that text as their session username and then echo a welcome "username" at the top of each page, and then a logout button which will end the session and allow for the user to input a new username
Currently the code will allow the user to input a name, and then a Welcome will appear for that name, but it will be destroyed if the user changes pages, and the textfield to input a name never disappears either - This is the master page in codeigniter
<form action="" method="POST">
<input type="text" name="input_value">
<input type="submit" name ='in' value="LogIn">
</form>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION['userses'] = '';
}
if(session_status() == PHP_SESSION_ACTIVE && $_SESSION['userses']=='' ){
$_SESSION['userses'] = $_POST['input_value'];
echo "Welcome, ".$_SESSION['userses']."!";
}
else{
echo "Welcome, ".$_SESSION['userses']."!";
}
?>
<form action="<?php session_destroy(); ?>" method="POST">
<input type="submit" name='out' value="LogOff">
</form>
You have to change
if(session_status() == PHP_SESSION_ACTIVE && $_SESSION['userses']=='' )
to
if(isset($_POST['in']) ){
$_SESSION['userses'] = $_POST['input_value'];
}
so when he submits username, this if will catch the post request and then add input to your username
you don't need to check session_status
edit:
and as the comments said, put session_start(); at the beginning of every page
example of code :
<?php session_start();
if(isset($_SESSION['userses'])) echo "Welcome".$_SESSION['userses'];
?>
<form action="" method="POST">
<input type="text" name="input_value">
<input type="submit" name ='in' value="LogIn">
</form>
<?php
if(isset($_POST['in']) ){
$_SESSION['userses'] = $_POST['input_value'];
}
?>
<form action="<?php session_destroy(); ?>" method="POST">
<input type="submit" name='out' value="LogOff">
</form>
You are using codeigniter, why not use session library :
https://www.codeigniter.com/user_guide/libraries/sessions.html

New to sessions in PHP

I'm trying to get to grips with sessions as it's a part of PHP I'm not very good with. Could you help me in explaining what is happening here on the two pages that I have? It is giving an undefined index and I've no idea why.
Thanks
File 1
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
File 2
<?php
session_start();
echo $_SESSION['picturenum'];
?>
session_start() must go at the top of the page:
<?php
session_start();
// Opening <html>, etc goes below
?>
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
}
?>
This works:
Form (teste1.php)
<?php
session_start();
// Opening <html>, etc goes below
?>
<strong>Test Form</strong>
<form action="test2.php" method"post">
<input type="text" name="picturenum"/> <!-- make sure you type something here -->
<input type="submit" name="Submit" value="Submit!" />
</form>
File 2 (test2.php)
<?php
if (isset($_POST['picturenum'])) {
$_SESSION['picturenum'] = $_POST['picturenum'];
echo $_SESSION['picturenum'];
}else{
echo "something wrong with the POST";
}
?>
As far as I can see, you're starting session after the form in the first file. The rule is: you should start the session before any echo or any HTML output, even before a space. So, basically, session_start() should be your first line after <?php.
Then, how do you get to the second page? If you close the browser and then re-open it, the session of course won't persist and you'll get your undefined index.
Please comment on this if you need any further explanations.

How to redirect with a $_SESSION variable from a form

I'm a bit inexperienced so go easy on me.
I need to save the value from a form textarea before the form is submitted (I need it even after the page is reloaded).
After the reload, I need to redirect to a predefined page on the site that includes the textarea value on the very end on the URL.
I have something like this so far:
<php?
session_start();
$_SESSION['textarea_value'] = $_POST['textarea_name'];
?>
// below is called directly after a popup form submission
location.reload();
if ($_SESSION['textarea_value'] != null) {
header("Location: http://www.xxxxxxxx.com/?s=$_SESSION['textarea_value']");
unset($_SESSION['textarea_value']);
}
Okay, so here's what I would do:
Method #1
index.php
<?php
if($_POST)
{
session_start();
$_SESSION["someVar"] = $_POST["someVar"];
header("Location:otherPage.php");
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
session_start();
$someVar = $_SESSION["someVar"];
?>
Method #2: If you want to do it with a get request, you don't even need to use sessions:
index.php
<?php
if($_POST)
{
header("Location:otherPage.php?someVar=".$_POST["someVar"]);
}
?>
<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_GET["someVar"];
?>
Method #3: You could also even take out the redirection entirely:
index.php
<form action="otherPage.php" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>
otherPage.php
<?php
$someVar = $_POST["someVar"];
?>

Session variables from form not showing on next page

I can set session variables and use them on another page. However when i try to use a simple contactform with a username and email address and try to store them into session variables, they don't show up on other pages. There must be something basic i'm missing.
Here's the form:
<?php
session_start();
$submit = $_POST["submit"];
if($submit){setSessionVars();}
function setSessionVars() {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header('Location: session.php');
}
?>
<html>
<body>
<form action="session.php" method"post">
<input name="name" type="text" value="Name" size="11" /><br />
<input name="email" type="text" value="Email" size="11" /><br /><br />
<input name="submit" type="submit" value="Submit" size="11" />
</form>
</body>
</html>
And this is session.php:
<?php
session_start();
echo $_SESSION['name'];
echo $_POST['name'];
?>
Also
header('Location: session.php');
is not working. Any ideas?
At a glance, I see one immediate problem that will keep the form from posting.
<form action="session.php" method"post">
You need an "=" sign between method and "post".
Changing that alone will give you the "t" in session.php.
You post the form to session.php:
<form action="session.php" method"post">
I'd change it to:
<form method="post">
That way, the page posts to itself. Then it can register the session variables and redirect the user to session.php.
Edit: also, you forgot the = sign in method"post".

Categories