I am setting a session variable when a submit button is pressed like so:
<?php
$submit = #$_POST["submit"];
if($submit){
$_SESSION['id'] = $id;
}
?>
<form action="add.php" method="POST">
<input type="submit" name="submit" value="Add">
</form>
However, in the page add.php when I do:
print_r($_SESSION['id'];
I get the following error:
Undefined index: id
I'm new to sessions so still trying to come to grips with them, but I thought the part where I do $_SESSION['id'] = $id; is where I define the index id to be the value of the $id variable? Could someone explain where I am going wrong?
UPDATE:
In case you're wondering if I am using session_start(): At the top of the page with the form, I include my header page which at the top contains require_once './init.php';. At the top of my add.php I just have the line require_once './init.php'; (I do not include the header file in add.php as this page will redirect as soon as it has executed its code.
In the init.php file is the following:
<?php
session_start();
require_once 'configurate.php'; //database info
?>
you should call session_start() before setting any values in $_SESSION
change your code like this
<?php
session_start();
$submit = #$_POST["submit"];
if($submit){
$_SESSION['id'] = $id;
}
?>
<form action="add.php" method="POST">
<input type="submit" name="submit" value="Add">
</form>
I hope you have initialized your session via session_start before setting and retrieving the value from $_SESSION
if the value submitted via $_POST["submit"] compares to false (e.g. "" or 0) your condition if($submit) won't succeed, better use if (isset($_POST['submit'] instead
Related
I just wondering if its possible to store form data as Session on Multiple Pages?
I have 5 Pages on my Site, every page has an form with an input field on it.
I want save all Sessions and input values on all 5 Pages, and echo it on the last page.
I had saved the session_start(); in one file and include it on every page:
session_start.php
<?php
session_start();
?>
On the top of page before <!DOCTYPE html> i add it like this on every page:
Page 1:
<?php
include("config.php");
require_once("session_start.php");
if (isset($_POST['submit'])) {
$_SESSION['email'] = $_POST['email'];
}
?>
then in the Body:
<form action="" method"post">
<input type="text" name="email"/>
<input type="submit" name="submit" value="Enter" />
</form>
Page 2:
<?php
include("config.php");
require_once("session_start.php");
if (isset($_POST['submit'])) {
$_SESSION['passwort'] = $_POST['passwort'];
}
?>
then in the Body again:
<form action="" method"post">
<input type="text" name="passwort"/>
<input type="submit" name="submit" value="Enter" />
</form>
I can echo the email Session on the Page 2 without Problems with:
<?php
echo $_POST["email"];
?>
But get an error on the page Undefined index: passwort if i do it on the same way as in Page 1.
And continue on the other 3 Pages, whats wrong with my Way?
With my code here i can save the Session only from the Page before and echo it on the next page.
Thanks for looking!
So long as you are successfully calling session_start(); at the start of each new page load, you will be carrying the previously store session data forward.
Yes, you will need to transfer the form-submission data from $_POST to the $_SESSION array each time.
After this snippet on page 1:
if (isset($_POST['email'])) {
$_SESSION['email'] = $_POST['email'];
}
Your session will contain:
$_SESSION = ["email" => "example#email.com"];
Then after this snippet on page 2:
if (isset($_POST['passwort'])) {
$_SESSION['passwort'] = $_POST['passwort'];
}
Your session will contain:
$_SESSION = [
"email" => "example#email.com",
"passwort" => "p#$$w()rt"
];
And so on, for the pages to follow.
When you want to check what is in your array, you can simply call var_export($_SESSION).
When you want to access particular elements, use their key.
E.g. echo $_SESSION['passwort'];
I have a simple HTML script:
<form action="" method="post">
<input type="text" name="amount">
<?php echo $amountError; ?>
<input type="submit">
</form>
And I display errors using this:
<?
$amount = $_POST['amount'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST['amount'])) {
$amountError = 'Empty amount';
} else {
header("Location: apmoketi.php");
}
?>
In the apmoketi.php file there is only one line
echo $amount = $_POST['amount'];
However, when I'm trying to display data in the apmoketi.php page, I don't get the value of the $amount variable using action="", but using action="apmoketi.php" I can get it but my PHP code doesn't checking for any errors, why?
A call to header("Location: apmoketi.php"); makes the browser follow the specified URL as another, completely separate request. This new request doesn't have access to the POST data of the previous one.
Your options:
use include so apmoketi.php has access to the variables of the current script
use sessions to preserve data between requests
use a query parameter to carry the value over:
header('Location: apmoketi.php?amount=' . urlencode($amount));
When you redirect to header("Location: apmoketi.php");, the variables are no longer in $_POST[] so you need to set a session variable before you redirect:
$_SESSION['amount'] = $amount;
the way i do it is i have a folder called action with subfiles in there lets say the file called file2 in this file all the php action happens.
and in the root of my folder i have another file called file1 and in here theres the html and now how i would do it is
<form action="action/file2.php" method="post">
<input type="text" name="amount">
<?php echo $amountError; ?>
<input type="submit">
</form>
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');
}
?>
I'm trying to save a session variable in one of my script and it seems to work fine for a portion of the script, however, it gets dropped in the second part of my code. I've echoed every line and can't figure out why this is happening. Also, this code was working just fine before, but ever since I switched servers, it seems to be malfunctioning. I don't think it's the session save path because the first part of the code works fine. Here is the basic outer structure of my code (I've removed all the irrelevant parts to save space):
<!--raw-->
<?php
session_start();
include 'config.php';
?>
<html>
<body>
<form id="driver_record" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
Enter Driver Number: <input type="text" name="driver" />
<input type="submit" value="Submit" name="user_submit" />
</form>
<?php
if(isset($_POST['user_submit']))
{
if(username_exists($_POST['driver'])){
$ulog = $_POST['driver'];
$_SESSION['user_id'] = $ulog;
echo $_SESSION['user_id']; //Session echos fine here
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
?>
<form id="expenses_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<input type="submit" value="Approve" name="record_approve" />
<?php
}
}
?>
<?php
if(isset($_POST['record_approve']))
{
echo $_SESSION['user_id']; //Session does not echo here
$ulog = $_SESSION['user_id'];
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
}
//unset($_SESSION['user_id']);
//session_destroy();
?>
</body>
</html>
<!--/raw-->
EDIT: I have commented out the unset statement, and also tried putting them inside the if statement, with no change to the behavior of the code.
Everytime the code is run you are calling the unset($_SESSION['user_id']); this is because it is not within your IF statement, you have a curly brace in the wrong place.
<?php
if(isset($_POST['record_approve']))
{
echo $_SESSION['user_id']; //Session does not echo here
$ulog = $_SESSION['user_id'];
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
unset($_SESSION['user_id']);
session_destroy();
} //moved curly brace here
?>
EDIT: The code runs fine for me, I had to comment out the include line and the function used, the fault must be within the config.php. Do you have a session_start() at the top of that file and no unsets used ?
Here is a simple session script, are you having problems with this ?
If so then there is a problem with php.
<?php
session_start();
$_SESSION['testing'] = "boo";
echo "Session 1=" . $_SESSION['testing'];
?>
<form action="session1.php" method="post">
<input type="text" name="input"
<input type="submit" />
</form>
<pre>
<?php
if (isset($_POST['input']))
{
print_r ($_SESSION);
print_r ($_POST);
}
?>
It seems that neither I or anyone else here can find a logical solution as to what's happening. I've decided to simply remove the session variables and include a 'hidden' input field that just sends the variable through a POST variable. This seems to solve all my problems without having to worry about the behavior of the session variables.
You set the session value only when the first form is submitted (isset($_POST['user_submit'])). When the script is processing the second form submission (isset($_POST['record_approve'])), no session value is set. Hence no output.
You do realize that, even if the script is in one PHP file, it will be triggered by two mutually exclusive form submissions, right? So the session value will be available only after isset($_POST['user_submit']. So if you submit the second form after submitting the first one, you will have a session variable 'user_id', provided you have commented out the lines that unset and destroy the session.
Hope it makes sense.
Suppose that I have also coded a similar login form then issue the session by name via $_SESSION['name'] as follows
session_start();
if(!isset($_SESSION['name'])){
header("Location: login.php");
}
then right on the same file (display.php) I also display a form to post a message to the administrator to tell him about how I feel such as
<td>
<form action="tellhim.php" method="POST">
Title:<input type="text" col="30" name="comment_title"/><br/>
Your feeling:<br/><textarea name="comment_content" col="10"></textarea><br/>
<input type="hidden" name="postfeeling" value="TRUE"/>
<input type="submit" value="Submit"/>
</form>
</td>
that means, right after I click the button to submit my feeling I will be directed to tellhim.php. The problem then is that the session seems invalid right after the page is reloaded. Could someone help me out please ?
You should exit(); after header()
file tellhim.php needs that also:
session_start();
AND the session_id has somehow to be added to tellhim.php, automagically like this:
ini_set('session.use_cookies', 1);
ini_set('session.use_trans_sid', TRUE);
ini_set('url_rewriter.tags', 'a=href,area=href,script=src,link=href,frame=src,input=src,form=fakeentry,form=post,form=action');
session_start();
You need to use session start() before all the files that you want to use
//login.php after login redirect to display.php
session_start();
//set session variable
$_SESSION['name'] = 'xxx';
//display.php
session_start();
if(!isset($_SESSION['name'])){
header("Location: login.php");exit;
}
{rest of the form code goes here}