This is more of a general question without specific code.
Under what circumstances can a $_SESSION variable in PHP simply vanish?
I have properly declared
<?php session_start(); ?>
without spaces at the very top of each page. On one page, I can refer to the session variable and everything's OK. Yet, when I go to the page that's a problem, and use
<pre> <?php print_r($_SESSION); ?> </pre>
I get no results - the session is empty.
Am I missing something obvious?
SOLVED. It had to do with the fact that there was a problem <body> tag (and /body) in the wrong place. Once moved/removed, it works.
Related
I am creating a set of 3 pages, each with a dropdown menu. The values from the previous dropdown menus will populate the next dropdown menu. I understand that I must use session variables to hold the value of each dropdown menu into later pages. As a simple test, I echo the variables to see if they have been carried over -- and that's where the problem is.
I have three different files: choose_cc.php, choose_uni.php, and choose_major.php
The value from the dropdowm menu in choose_cc.php does get echoed in choose_uni.php -- however the value from choose_cc.php does NOT get carried over into choose_major.php -- despite me storing it into a session variable.
the flow of pages is like this;
choose_cc.php --> choose_uni.php --> choose_major.php
Each php file has it's own form. The problem lies in when I try to call the value from choose_cc.php into choose_major.php, i have issues.
The name of the form on choose_cc.php is choose_cc.
The name of the form on choose_uni.php is choose_uni.
So for example, in choose_uni.php, I retrieve the value from the dropdown menu on the previous page (choose_cc.php) like this:
$_SESSION['choose_cc'] = trim($_GET['choose_cc']); //fetches cc from previous page
$user_cc = $_SESSION['choose_cc'];
echo $user_cc;
and when I echo it as I did above, it works! Okay perfect!
But when I head onto choose_major.php, I try retrieving the value again from the form, but to no avail like this;
echo $_SESSION['choose_uni']; //this works
echo $_SESSION['choose_cc']; //this doesn't work
I have made sure to store to do session_start() on the beginning of each page as well.
Please help me out! this is driving me insane!
Create a new "see.php" with this:
session_start();
print_r($_SESSION);
and execute it after each choose_cc.php, choose_uni.php and choose_major.php to take a look the session you have after you run your programs.
Simple as this:
a) add session_start(); at the beginning of ALL the involved pages. Some weird stuff happens sometimes if you put a space before it, or even a new line. So be sure it is the very first thing in your script.
<?php
session_start();
?>
b) if needed, check the variable to save into session for not empty(); That way you can be sure the session variable contains something, unless of course you want explicitly to be empty.
c) then load the session variable. You can temporary check it with a var_dump();
<?php
session_start();
if (!empty(trim($_GET['choose_cc'])))
{
$_SESSION['choose_cc'] = $_GET['choose_cc'];
}
var_dump($_SESSION['choose_cc']);
?>
I need some help on passing a url php variable onto the next page. I've tried searching throughout the site for help and I've spent a lot of time trying to figure this out with no luck. Basically I need to be able to change the paypal link button id on page 2 with the url variable from page 1.
The variable is initially passed along with the URL: http://www.example.com?p=paypalbuttonid
I would like to store and pass that "p" variable on to the next page. I don't want to pass the variable onto page 2 with a link. I would prefer to store the variable and recall it on page 2.
Page 1 code (above html):
<?php
session_start();
$_SESSION['paypal'] = $_GET['p'];
?>
Page 2 code (above html):
<?php
session_start();
$p = $_SESSION['paypal'];
?>
I'm calling the variable in a link on page 2 (body):
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=<?php echo $p ;?>" target="_blank" class="btn">
I'm not sure what I'm dong wrong but I'm a complete newbie to PHP so please help! The variable shows up blank in the URL on page 2. Thank you! - Chad
First, you should make sure you dont have any output before session_start(), it is safe to put session_start () at the top of the page , especially if you use php code in .html, since you may have output without awareness, and session wont work if you have anything output to the browser before session_start()
according to php.net:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
and you should check if you cookie is enabled.
Secondly, var_dump ($_SESSION); to see if you can get anything
I created a session variable in a php page 'session1.php' as follows
<?php
session_start();
$_SESSION['name']="piklu";
?>
now I want to access the session value in another page say 'session2.php'.i need suggestion for that.
what I have done in 'session2.php' is as follows.
<?php
include('session1.php');
echo $_SESSION['name'];
?>
The above code is working but only when session1.php and session2.php doesn't contain any html tags.if it is then all html tags from 'session1.php' will be copied to 'session2.php'. Is there any other way to do that?
You don't need to include all of session1.php. instead, just call session_start() at the start of session2.php.
I've written a bit of code to upload a file to a server. When the file is successful I made my code go to another web page. On this web page I want to print the file properties which were gained from the previous page so I am using Sessions in PHP.
//Starts up a new PHP session
session_start();
$_SESSION['file']=$_FILES["file"];
$_SESSION['name']=$_FILES['name'];
$_SESSION['type']=$_FILES['type'];
$_SESSION['size']=$_FILES['size'];
$_SESSION['tmp_name']=$_FILES['tmp_name'];
That is my session on page1. Then, when the file is successful, I send the user to page2.
header( 'Location: page2' ) ;
Now, on page2, I have this right at the top of my .php page:
<?php
//Starting session
session_start();
?>
Then, further down for me to be able to print each variable out I've got:
<?php
Print_r ($_SESSION['file']);
?>
I get all my information all jumbled into a long sentence when this is done. I want to have control over the information and print it nice and neatly. What am I doing wrong? I've researched into loads of different ways of doing this and nothing has helped so far.
Tried that, also tried just printing SESSION, also tried echoing each one seperately with the words [Array] being printed only.
Thanks in advance!
Use two dimensional array like below
$_SESSION['upload1']['file']=$_FILES["file"];
$_SESSION['upload1']['name']=$_FILES['name'];
$_SESSION['upload1']['type']=$_FILES['type'];
$_SESSION['upload1']['size']=$_FILES['size'];
$_SESSION['upload1']['tmp_name']=$_FILES['tmp_name'];
on file2.php
<?php
print "<pre>";
print_r($_SESSION['upload1']);
print "</pre>";
?>
You are dealing with white-space breaks vs HTML breaks.
Just print it as <pre> and you're good to go.
<pre>
<?= print_r ($_SESSION['file']); ?>
</pre>
print_r() will print out your information in a nice ASCII-Art like format. The HTML engines render this unreadably. Chose "view source" (Ctl-U) in your browser to view it in a human-understandable form.
You can also install Xdebug which will make really nice formatting : http://xdebug.org/docs/install
and see: https://stackoverflow.com/search?q=how+to+install+xdebug if you have any issues installing xdebug
Use a table to display the data (or am i missing something !?!) :
<table>
<tr><td>File Name</td><td><?php echo $_SESSION['name'] ?></td></tr>
<tr><td>File Size</td><td><?php echo $_SESSION['size'] ?></td></tr>
</table>
add whatever extra info you need ...
I have a form that is supposed to store some of its data into a session. For the purpose of this example.
This information is then collected on another page.
<?php
session_start();
print_r($_SESSION['booking-form']); ########### for debugging purposes ###########
if ($_SESSION['booking-form']) {
echo $_POST['GT_title']
?>
<!DOCTYPE html>
<html>
<?php
}
elseif ($_SESSION['booking-form']) {
}?>
</html>
Now although the debug is working the echo is not working ie. echo S_POST['GT_title'].
How do I echo the information on the second page.
Can I echo it wherever I want it in the html (ie. in the middle of the body somewhere with
On the first page some data in the form can be changed with options. This would need to update the session before the page is changed.
CLARIFICATION
For the purpose of a booking form. The user filled in a series of options that were then echo'd on the next page. At this point they need to log in so the form data must enter into a session. The session data should then be returned on a third page.
1) what information? $_POST['GT_title'] would refer to a form element, presumably on the previous page. Does it exist? Is it filled in? what are you expecting?
2) You can echo where ever you like.
3) clarify? you can update a session with post values easily.
$_SESSION['foo'] = $_POST['bar']
your question isn't really that clear.
You can echo it wherever you want but if it doesn't exist or doesn't have a value then it might appear to be not working. To know for sure use var_dump($_POST['GT_title']);
For your echo question: Are you sure that GT_title is set? Posting the form on your previous page might help.
You can echo wherever you want, you can just put
<?=$varname?>
anywhere in your html.
You can also update your session wherever you want.
$_SESSION['abc'] = value;
echo '<pre>';
print_r($_POST);
echo '</pre>';
Print it after your header.
I hope this helps a little :)