I'm writing a php code processing a lot of data, sometimes interactively. In my starting php page i call the function session_start() before sending any other data to the browser. Then, i put some data into the $_SESSION[] array, like this:
$_SESSION['something'] = $variable;
After, there is a form, what is sent via GET and XMLHttpRequest.
getrequest.open("GET", "data_processing.php?var="+onevalue+"&another_var="+twovalue, true)
getrequest.send(null)
Another php script recieves the user data from this GET, and there i'd like to use the data stored in $_SESSION as well. But $_SESSION seems to be empty. I've never used sessions, what is the correct way to make available variables to consequent php scripts?
Are you sure you called session_start first?
See: http://php.net/manual/en/function.session-start.php
I finally found the solution: another script still used the session, so i had to insert session_write_close(); then it works fine. Thank you for all the answers!
Related
Currently I put a constant on my webpage using PHP which I then send, with Ajax, to my POST function. However, this leaves it susceptible to hacking (the user could change the variable with Firebug), so is there a way to store the variable in the PHP of the page and then access it later on, in the POST method (or is the GET variable of the page still available in the POST function, since that's where I get the variable from)?
I think what you have wanted is to store the post value to use it later.
Here you would need to use $_SESSION
You can do it like
session_start();
// Save variables into session
$_SESSION['thevalue'] = $_POST['value'];
If you wish to store between successive calls from the same user use the follwing:
<?php
session_start();
$_SESSION["your variable/constant"] = yourvaule;
Now use the variable as needed, accessing it as $_SESSION["your variable/constant"]
Hope it helps, and it's what you're asking.
While using the sessions in my PHP script I wanted to pass the session variable to the PHP called script. But the session variables are not being called as the session_start() function could not be used after the HTML code. I am using the simple Javascript AJAX. Please provide me the path.
I think what you need is the following:
in your php-script you open/request with ajax, you have to add in the first line:
session_start(session_id());
This way, you have access to the variables you stored in your session where you called the request with ajax
Consider the following scenario. I have three php files, file1.php, file2.php and file3.php located on my server.
file1.php starts a session and sets a session variable say,var.
I am able to access var using $_SESSION['var'] in file2.php. file3.php is called using jquery ajax functionality, but im unable to access $_SESSION['var'] in file3.php. if i do a gettype($_SESSION['var']) in file3.php it returns NULL.
What could be the problem here ?
Please help
Thank You
From my tests it should work. What could be happening is if you do not have sessions set to use cookies and they are being appended to the url, you would need to pass the session hash via GET, using the proper name set in the php.ini, to the uploadify script.
But there are a bunch of inconsistencies, especially in that pastie, you do not necessarily have to rename every part of your code, just need to post the relevant sections.
I'm almost embarrassed to ask because it seems so simple, but I can't get it to update.
When the user logs in I set the session vars like
array('users'=>array('timezone'=>'America/los Angeles'));
I can then retrieve the data as follows: $_SESSION['users']['timezone']
and it works fine.
However in the user profile page the user can change their timezone and when I try to update the $_SESSION as follows it doesn't work:
$_SESSION['users']['timezone'] = 'America/Denver';
What am I doing wrong?
--- More code as requested -------
I found that the session variables were being set by a function inside of a class
Here's the function:
function session_var_register($object_name, $var_value)
{
$_SESSION[$object_name]=$var_value;
}
Here's how the function got called:
$gf->session_var_register("users", $user_array)
Users Array looks like array('users'=>array('timezone'=>'America/los Angeles'));
I don't understand why this doesn't work. I was able to get around the problem though by bypassing the function call and just creating the array like:
$_SESSION['users'] = $user_array;
But for knowledge reasons and if anyone else comes along this post, could anyone explain what the function was doing different? There were no errors thrown, just would not allow me to assign anything to the session variable once it was registered via the function...almost acted like it became read_only once instantiated.
Make sure you session_start() on every page that accesses the $_SESSION variable.
Sounds like the code that updates it may not be getting executed. You might try putting some kind of debugging statement before this assignment like an echo to verify that the action is being taken.
Following on from Scott's reply, double checking your session is started is a good "start".
There's a good tip here which you may find useful in debugging.
re-initialize your session id. That way you are sure it has a new spanking id to store your variables.
Are you doing a redirect soon after this code?
Do a session_write_close() before doing a redirect so that session vars are stored again before redirecting.
I'm trying to use the header() function to create a redirect. I would like to display an error message. Currently I'm sending the message as a parameter through the URL, however this makes it look quite ugly.
Is there a way to pass this value as a post variable instead?
Any advice appreciated.
Thanks.
Dan, You could start and store a session in PHP then save the message as a session variable. This saves you from having to transfer the message in an HTTP request.
Manipulating Sessions
//Start the session
session_start();
//Dump your POST variables
$_SESSION['POST'] = $_POST;
//Redirect the user to the next page
header("Location: bar.php");
Now, within bar.php you can access those POST variables by re-initiating the session.
//Start the session
session_start();
//Access your POST variables
$temp = $_SESSION['POST'];
//Unset the useless session variable
unset($_SESSION['POST']);
To read more about sessions, check out: http://php.net/manual/en/function.session-start.php
The header function is used to send HTTP response headers back to the user so actually you cannot use it to create request headers :(
One possibility is to use the CURL but I don't think it is worth of what you are doing.
Provided that you have local access to the page displaying the error, instead of redirecting you could include it in the page which caused the error and then programmatically display the error message.
if(something_went_wrong()) {
require_once('errors.php');
display_error('something really went wrong.');
}
The errors.php file would then contain a definition for display_error($message), which displays the formatted message.
When passing variables between modules I have found it easier to construct an array from the variables, convert the array to json and store it in a db table with two columns, a varchar key and a text data. The json would go in data and the key could be anything you want. Then in the target module you just read that back, convert the json back to an array and voila, you have your variables. No $_POST, no $_SESSION, no fuss, no muss, quick and easy. Of course that assumes you have access to a database, although you could use a file on the server. $_POST is useless since it needs a and $_SESSION can be cranky and can lead to unexpected results. Otherwise you'd almost have to use ajax.