echoing session variable and then unseting session variable dont show session variable - php

page1.php
$_SESSION['sVar'] = 'XYZ';
header('Location:page1.php');
page1.php
echo $_SESSION['sVar']; // Works well
page1.php
echo $_SESSION['sVar']; // donot get displayed after adding following line
unset($_SESSION['sVar']);
My Question is : When I am echo session variable it works well but when i destroy that variable after echo its not getting echo.

This works fine for me
session1.php :
<?php
session_start();
$_SESSION['sVar'] = 'somecrap';
header("Location: session2.php");
exit;
session2.php :
<?php
session_start();
echo $_SESSION['sVar'];
unset($_SESSION['sVar']);
Obviously if I refresh session2.php I get an undefined index warning
Are you starting the session in both pages?

Related

Using $_SESSION to carry a php variable from one page to the next?

I created an app to receive and tally votes. I am trying to make the voter's selection carry over to a confirmation page so that I can display the name of the candidate for whom they voted on the confirmation page.
I am trying to use $_SESSION on the variable of the selected candidate from the voter's submission, and then call the variable on the confirmation page, however I continue to get undefined variable error.
voterSubmit.php:
<?php
$selectedCandidate = $_POST['candidateid'];
session_start();
$selection = $_SESSION[$selectedCandidate];
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$_SESSION[$selectedCandidate] = $selection;
include "views/confirmation.php";
confirmation view:
<?php include "../partials/header.php"; ?>
<h1>Thanks For Your Vote!!</h1><br>
//This is where the error occurs (on my selection variable):
<h2>You voted for <?=$selection?>
View Results
<?php include "../partials/footer.php"; ?>
I want the name of the selected candidate to appear on the confirmation page by way of the $selection variable. However, all I receive on the front end is an "undefined variable" error. I also would like to note that instead of using the $selectedCandidate variable in my session, I have also tried grabbing the name directly by just using the name of the radio button selection as such:
$_SESSION['candidateid'] = $selection
I also would like to mention that i have tried the reverse:
on confirmation.php:
session_start();
$selection = $_SESSION[$selectedCandidate];
on voteSubmit.php:
session_start();
$_SESSION[$selectedCandidate] = $selection;
Your using the $_SESSION variable incorrectly.
Try:
voterSubmit.php:
<?php
$selectedCandidate = $_POST['candidateid'];
session_start();
$_SESSION['selectedCandidate'] = $selectedCandidate;
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];
include "views/confirmation.php";
voterSubmit.php looks ok... but I don't understand why you have include "views/confirmation.php"; in the confirmation.php file.
<?php
session_start();
$_SESSION[$selectedCandidate] = $selection;
include "views/confirmation.php";
Try coding your HTML/PHP this way:
<?php
/* confirmation.php */
session_start();
$_SESSION[$selectedCandidate] = $selection;
require_once("../partials/header.php");
//This is where the error occurs (on my selection variable):
echo <<<_STRT
<h1>Thanks For Your Vote!!</h1><br>
<h2>You voted for $selection</h2>
<p>View Results</p>
_STRT;
require_once("../partials/footer.php");
?>
I essentially eliminated a lot of start/stop code. The start of your PHP recalls the session and variable. Then I go into the HTML portion to provide the results to your site visitor. Give it a shot. Comment on it if it doesn't resolve your issue so we can rethink it.
I think session_start() needs to be the first line after the php tag.
$_SESSION is an array. So you need to assign it values like:
$_SESSION['keyname'] = $value;
voterSubmit.php:
<?php
session_start();
$_SESSION['selectedCandidate'] = $_POST['candidateid'];
//Redirect to results page
header("Location: views/confirmation.php");
confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];
include "views/confirmation.php";
There are 2 issues in your code :
session_start() defined in the wrong place.
Wrong way of assigning value to your session variable.
Answer :
session_start() should be the first thing defined after your php
tag.
Correct way to declare session variables is : $_SESSION["selectedCandidate"] = $selectedCandidate; where, $selectedCandidate is the value to be assigned to your session variable, named selectedCandidate.
You have done 2 mistakes in your code:
1. Session should be defined in starting of page it means you have to define it like this:
<?php
session_start();
//php code goes here.
?>
2.wrong initialization of session variable .you should do it like this:
<?php
$_SESSION['selectedCandidate']=$selectedCandidate;
?>

could not get php session variables to other page even I use session_start() at top

I have been stuck to get session data on second page, following are sample code for both pages, but on second page I can't get any value in session
for a.php
<?php
session_start();
$_SESSION["captchacode"] = 123456;
?>
for b.php
<?php
session_start();
echo "<pre>";
print_r($_SESSION);
var_dump($_SESSION);
die;
?>

How to pass variable to another page without include

Hey guys I'm trying to pass a php variable to another page. I tried it with sessions but no result.
newspaper.php
$newspaper= $newspaper['newspath'];
print_r($newspaper);
this outputs:
path/to/the/newspaper.
Now I want to use the variable in the second page.
newspaperviewer.php
echo $newspaper;
$SESSION = $newspaper;
I tried the first one but no result. The second one seems to be faulty.
Hope you guys can help me out.
Session is what you are looking for. A session variable can store a value and use this value on all pages of your project.
First thing to do is to start session on each file on your project. You can do this like this example
<?php
session_start(); //declare you are starting a session
$_SESSION['newspaper'] = "New York Times"; //Assign a value to the newspaper session
?>
On the other file you can use the value of the session by trying something like this
<?php
session_start(); //always start session don't forget!!
echo $_SESSION['newspaper'];
// This will echo New York Times
?>
Store the variable after starting session on page A, like so:
// FIRST PAGE (foo.php)
session_start();
$_SESSION['name'] = 'Jack';
Now, on the second page (or any page that you want to have access to $_SESSION, simply do the same but pull the variable.
// SECOND PAGE (bar.php)
session_start();
$name = $_SESSION['name'];
$_SESSION['name'] = null; // Or use session_unset() to delete all SESSION vars.
And that's how you pass variables using $_SESSION.
Please use this code to set session
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
You can write like this
newspaper.php
session_start();
$newspaper= $newspaper['newspath'];
$_SESSION['newspaper'] = $newspaper;
Now you can use this session variable in
newspaperviewer.php
session_start();
$newspaper = $_SESSION['newspaper'];
echo $newspaper;
session_unset(); // remove all session variables
First
newspaper.php
$newspaper= $newspaper['newspath'];
//print_r($newspaper);
session_start(); //it starts your session here
$_SESSION['newspaper']=$newspaper; //it sets a session variable named as newspaper
Second
$newspaper= isset($_SESSION['newspaper'])?$_SESSION['newspaper']:''; //checks and sets value
echo $newspaper; //outputs value
For more see session_start
http://php.net/manual/en/session.examples.basic.php
First you will need to start the session by using session_start() at the top of your page. Second, a session variable is written like this: $_SESSION['foo'].
I suggest you read these pages to get a better understanding of what's going on.
http://php.net/manual/en/reserved.variables.session.php
http://www.w3schools.com/php/php_sessions.asp

PHP session not working page by page

I have two different page with below code. I trim whole code because more than 1000 lines and therefore trim what I did.
In the first page I remove session and fill with new array.
firstpage.php
session_start();
unset($_SESSION);
$_SESSION['new'] = 'test';
print_r($_SESSION); // Session Working fine here at the bottom of page.
secondpage.php
session_start();
print_r($_SESSION);
But in second page show empty array()! I don't have any idea why such this happen
First page header:
PHPSESSID e73jddq9fqhaeeav346h724js7 xxx.ttt.com 35B / Session
Second page header:
PHPSESSID e73jddq9fqhaeeav346h724js7 xxx.ttt.com 35B / Session
both is same.
The script works fine on windows PHP but in cpanel(linux base) doesn't work!
Please test this code :
firstpage.php
<?php
session_start();
$_SESSION['name'] = "john doe";
echo $_SESSION['name'];
?>
secondpage.php
<?php
session_start();
echo $_SESSION['name'];
?>

How can I clear my php session data correctly?

My php website flows like this:
Page1.php has an html form which POSTs to Page2.php
Page2.php stores all the POST data into SESSION variables and has a button leading to Page3.php
Page3.php has another form which POSTs its data to Page4.php
Page4.php then stores all its POST data into SESSION variables
My problem is that it may be nessicary for a user to click the back button on Page4.php to go back to Page3.php and change some input. AS im sure your all aware when they get back to Page3.php the form will be blank as the entire page is re-rendered in its default state.
To get around this and re-display the user's previous input im doing this:
<input value="<?php echo $_POST["guest1Ticket"];?> " type="text" name="guest1Ticket" id="guest1Ticket" onblur="isTicketNumber(this)" size ="22"/>
This being the important part - <?php echo $_POST["guest1Ticket"];?>
This works but creates another problem for me. If the user goes back to Page1.php (before colsing their browser) and starts the process over again when they get to Page3.php the data from their last run through will be loaded into the form.
What I figure I need to do is clear all of the sdession variables when the user visists Page1.php. I tried to to that like this:
<?php
session_start();
session_unset();
session_destroy();
?>
(The above is at the very top of my file with no whitespace before the first character.)
No Warnings are generated when Page1.php loads but the session variables are not getting unset. When I get to Page3.php the data from the last run is still being entered into the form.
How can I clear my session data correctly?
BTW I only need this to work in Chrome and thats where im testing.
Only use session_unset() for older deprecated code that does not use $_SESSION.
see session_destroy manual
example you can try and see how it works
session.php
<?php
session_start();
$_SESSION = array('session1'=>1,'session2'=>2);
echo $_SESSION['session1']; //1
$_SESSION['session1'] = 3;
echo "<pre>";
print_r($_SESSION); //session one now updated to 3
echo "</pre>";
$_SESSION = array();
if ($_SESSION['session1']) {
echo $_SESSION['session1']; // IS NOW EMPTY
} else {
echo "woops... nothing found";
}
?>
<p>
<a href="destroyed.php">NOW GOING TO DESTROYED PHP<a/>
</p>
<?php
session_destroy();
?>
destroyed.php
<?php
session_start(); // calling session start first on destroyed.php
print_r($_SESSION); // prints Array ( )
?>
From the documentation:
If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used,
use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);
And take care about session_destroy:
session_destroy destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session
Use session_unset(). Like this:
<?php session_start(); ?><!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["variabletounset"] = "I am going to be unset soon along with all of the other session variables.";
print '<pre>' . "\n";
print_r($_SESSION);
print ' </pre>' . "\n";
session_unset();
print ' <pre>' . "\n";
print_r($_SESSION);
print ' </pre>' . "\n";
?>
</body>
</html>
This would output:
Array
(
variabletounset => I am going to be unset soon along with all of the other session variables.
)
Array
(
)

Categories