Im trying to figure out how to use a php $_SESSION value. The situation is:
Main HTML site has an <iframe> that loads products.php
Within products.php a form submits and saves the two values below:
$_SESSION["subtotal"] = $_POST['SUBT'];
$_SESSION["tax"] = $_POST['TAX'];
These work well I can echo them no problem (within that page)
This page replaces products.php with confirmation.php
Once in the confirmation.php I am not able to grab the session values. I have tried the following:
<?php
session_start();
$_SESSION["subtotal"] = $_POST['SUBT'];
$_SESSION["tax"] = $_POST['TAX'];
echo $_SESSION["subtotal"];
echo $_SESSION["tax"];
echo $subtotal;
echo $tax;
?>
AND
$_SESSION["subtotal"] = $subtotal;
$_SESSION["tax"] = $tax;
echo $_SESSION["subtotal"];
echo $_SESSION["tax"];
echo $subtotal;
echo $tax;
?>
AND
$subtotal = $new_subtotal;
$tax = $new_tax;
echo $new_tax;
echo $new_subtotal;
?>
All have failed to see $_SESSION - Is the value getting lost with the <iframe> change?
I have used the following code on the products.php page to confirm the session were susecssfully created - They are:
echo $_SESSION["subtotal"]." stored in session <br />";
echo $_SESSION["tax"]." stored in session <br />";
NOTES:
Both confirmation & products.php are on the same server.
print_r($_SESSION); Returns BLANK array.
session_start() is at the very top of both pages
Use something like this:
products.php
<?php
session_start();
$_SESSION['myValue']=$_GET['YourFormElement'];
//or
$_SESSION['myValue']=3; // You can set the value however you like.
?>
Any other PHP page:
<?php
session_start();
echo $_SESSION['myValue'];
?>
A few notes to keep in mind though: You need to call session_start() BEFORE any output, HTML, echos - even whitespace.
You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.
The setting of the variable itself can be done in one of a number of ways:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
And if you want to check if the variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}
I did that once by adding $_SESSION to url inside iframe and in file that is used in iframe I used that $_SESSION by $_GET.
I am not sure 100% but it should work as:
$session = $_SESSION['session'];
<iframe src="somePage.php?s=<?php echo $session; ?>"></iframe>
somePage.php
if(isset($_GET['s'])){
$session = $_GET['s'];
}
Related
I have following problem, I am trying to set cookies without any success. setcookie(); function returns true so it looks like it setting cookie however when I am trying to access it on the same or following page I get error 'Undefined Index....'
<?
session_start();
ob_start();
echo setcookie("order",$_SESSION['cart'],time()+3600,'/',NULL);
//added to see if Cookie is set
echo "<br/>";
var_dump($_COOKIE);
exit();
if($_GET['paypal'] == 1){
header("Location: /paypal-express-checkout/process.php");
}else{
header("Location: /insert_order.php");
}
ob_end_flush();
exit();
?>
next page follows like this
<?php
session_start();
include_once("../includes/inc_config.php");
include_once("../order.php");
include_once("config.php");
include_once("paypal.class.php");
#region POST
if(!isset($_GET['token'])) //Post Data received from product list page.
{
//Mainly we need 4 variables from an item, Item Name, Item Price, Item Number and Item Quantity.
if(!isset($_COOKIE['order'])){
exit();
}
$paypal_data = '';
$ItemTotalPrice = 0;
$order = unserialize($_COOKIE['order']);
print_r($order);
exit;
You are setting the domain value to NULL. Try leaving the NULL away:
echo setcookie("order",$_SESSION['cart'],time()+3600,'/');
OR set it to your domain:
echo setcookie("order",$_SESSION['cart'],time()+3600,'/',".yourdomain.com");
I would var_dump or print_r the $_COOKIE variable before I make a decision that it's not getting passed through. Holding that thought once you setcookie something gets registered into the $_COOKIE variable for sure.
I agree with the statements above since you only can access $_COOKIE on the next refresh but there is another way to do it to make your form or page more interactive.
I would register the cookie and use a php page refresh (display a working... div while thats happening) then come back to the page and try to do what you originally tried doing. Very basic but pretty much straight forward.
I want to pass a couple of variables from one PHP page to another. I am not using a form. The variables are some messages that the target page will display if something goes wrong. How can I pass these variables to the other PHP page while keeping them invisible?
e.g. let's say that I have these two variables:
//Original page
$message1 = "A message";
$message2 = "Another message";
and I want to pass them from page1.php to page2.php. I don't want to pass them through the URL.
//I don't want
'page2.php?message='.$message1.'&message2='.$message2
Is there a way (maybe through $_POST?) to send the variables? If anyone is wondering why I want them to be invisible, I just don't want a big URL address with parameters like "&message=Problem while uploading your file. This is not a valid .zip file" and I don't have much time to change the redirections of my page to avoid this problem.
Sessions would be good choice for you. Take a look at these two examples from PHP Manual:
Code of page1.php
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or pass along the session id, if needed
echo '<br />page 2';
?>
Code of page2.php
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
// You may want to use SID here, like we did in page1.php
echo '<br />page 1';
?>
To clear up things - SID is PHP's predefined constant which contains session name and its id. Example SID value:
PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1
Here are brief list:
JQuery with JSON stuff. (http://www.w3schools.com/xml/xml_http.asp)
$_SESSION - probably best way
Custom cookie - will not *always* work.
HTTP headers - some proxy can block it.
database such MySQL, Postgres or something else such Redis or Memcached (e.g. similar to home-made session, "locked" by IP address)
APC - similar to database, will not *always* work.
HTTP_REFERRER
URL hash parameter , e.g. http://domain.com/page.php#param - you will need some JavaScript to collect the hash. - gmail heavy use this.
<?php
session_start();
$message1 = "A message";
$message2 = "Another message";
$_SESSION['firstMessage'] = $message1;
$_SESSION['secondMessage'] = $message2;
?>
Stores the sessions on page 1 then on page 2 do
<?php
session_start();
echo $_SESSION['firstMessage'];
echo $_SESSION['secondMessage'];
?>
Have you tried adding both to $_SESSION?
Then at the top of your page2.php just add:
<?php
session_start();
Use Sessions.
Page1:
session_start();
$_SESSION['message'] = "Some message"
Page2:
session_start();
var_dump($_SESSION['message']);
In MVC, you can pass variable one page to another like this:
<?php $this->load->view('Overview', ['customer' => $customer , 'job_id' => $job_id , 'email' => $emailid]);?>
In Overview.php page you can display variable data like this
echo $customer; // it will display customer value
echo $job_id;
echo $email; // it will display email id
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
(
)
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?
First timer to the site, not an overly experienced php programmer here :)
I have a problem, i am using an iframe within a site which im attempting to use a session variable inside, firstly ive just been trying to display the session variable to make sure they are accessible from within the iframe:
echo "session of productcheck ".$_SESSION['productcheck']." ";
echo "session of productcheck1 ".$_SESSION['productcheck1']." ";
echo "session of productcheck2 ".$_SESSION['productcheck2']." ";
echo "session of productcheck3 ".$_SESSION['productcheck3']." ";
this just shows "session of product check" with nothing after each one, I set the session variable as such:
$_SESSION['productcheck'] = $productBox;
the $productBox is a GET from the URL:
echo " <iframe src=\"homeview.php?productBox=$product1\" name=\"FRAMENAME\" width=\"594\" height=\"450\" scrolling=\"No\" id=\"FRAMENAME\" allowautotransparency=\"true\" > </iframe >";
What's strange is if i just take the $productBox variable retrieved from the URL and use that then the code works, its only when i store it in a session variable that it gets confused. I want to retrieve a second $productBox and assign it to session var productcheck1 and so forth down the line. Unfortunately, i have to take one var in at a time, otherwise i could just pass all 4 products and not worry about the sessions.
Perhaps I'm making this far too complicated, any help would be much appreciated Thank you!!
You have to use session_start() in both scripts, the one setting the values (and presumably printing the <iframe>-element?) and the script that produces the contents for the iframe.
e.g. the "outer" script
<?php // test.php
session_start();
$_SESSION['productcheck'] = array();
$_SESSION['productcheck'][] = 'A';
$_SESSION['productcheck'][] = 'B';
$_SESSION['productcheck'][] = 'C';
session_write_close(); // optional
?>
<html>
<head><title>session test</title></head>
<body>
<div>session test</div>
<iframe src="test2.php" />
</body>
</html>
and the script for the iframe content
<?php // test2.php
session_start();
?>
<html>
<head><title>iframe session test</title></head>
<body>
<div>
<?php
if ( isset($_SESSION['productcheck']) && is_array($_SESSION['productcheck']) ) {
foreach( $_SESSION['productcheck'] as $pc ) {
echo $pc, "<br />\n";
}
}
?>
</div>
</body>
</html>
Not sure what's up with your session variables, but you can definitely pass all four variables through the url in your iframe. You just need to separate your key value pairs with an ampersand. So something like this:
file.php?key1=val1&key2=val2&key3=val3 and so on.
This is probably a better way than using session variables if your just trying to get data into that other file.