PHP $_SESSION lost over several pages - php

I am setting session_start() on every page as very 1st statement.
The 1st page sets $_POST['myVar'] when the page ist submitted. The 2nd page evaluates this by a php lib which sets $_SESSION['myVar'] if this is set in $_POST and not set already in $_SESSION.
When submitting this form and calling the 3rd one evaluating there $_SESSION['myVar'] results that the variable is no more set! There is no session_unset() called between.
Theoretically all is correct but the use shows the opposite. What may there be wrong or still missing?

In the page you have a form, probably your first page session is not required. In the next page(form action page), you need to start the session first. Then you can give like $_SESSION['fieldName']=$_POST['fieldName']. After this in whichever pages you use session_start(), you can have the data. Hope this helps..

Related

How can I destroy specifics $_SESSION variables leaving page?

I would like to unset some specifics $_SESSION variables after leaving a page, but the problem is that I have used pagination (using $_GET) for all sections of my site (contacts.php?page=1, ?page=2, etc; actions.php?page=1,?page=2 etc .; customers.php?page=1,?page=2, etc ...).
For all the sections there are filters which, if set, are saved in some $_SESSION variables. Those variables must remain set when passing from ?page=1 to ?page=2 etc but it must be deactivated by switching from contacts.php to actions.php, otherwise in actions.php I will have the already setted filters that I had set in contacts.php!
Thanks!
PS Sorry for my english
You can set the key name to null or ''.
For example
$_SESSION['key_name']='';
$_SESSION['key_name']=NULL;
sessions are recognized by a session id. if you delete this id the whole session (with all these entries) will be deleted. Therefore you could set the specific values to "null" and delete them this way.
you could do this either on the next page load or with javascript "on unload".
To remove a session on some specific pages redirections pass some variable to the target page and check on that page if the key exists then use unset function
unset($_SESSION['key_name'])
In case you want to remove session completely use
session_destroy();

Where do I end a PHP session so that a variable, created in the first PHP file, can be used in the second file?

I have a website where the user starts on one page and then moves to a second page. The first page initializes and adds values to a counting variable, which I then want to be able to be displayed on the second page. I know to use a session, but I am wondering how I should properly end the session--do I end it on the first page, or the second page? Do both pages need to begin with session_start()?
Yes. Both must use session start. The variable you are talking about is basically termed cookies. The variables will be shared throughout the browser sessions.

PHP Session variable changing from page to page

I'm using a session variable to save the last page the user visited in the search results so that they can go back to that page.
if I make
echo($_SESSION['page'])
at he end of the results pages the value is correct, but when I reload the page (or load any other page) the value is increased by 1. I made a echo($_SESSION['page']) right after the session_start() call and it was already increased, so I guess it's been increased right before the PHP code for the search results finishes running but can't find the line of code that is doing so.
In order to test this added a new variable called $_SESSION['page2'] and this new variable is been increased too.
Any ideas?
EDIT
The variable is taking the value from a Pager object like this
$_SESSION['page'] = $pager->getCurrentPageID();
And I can't find any other place where its been set to a different value. We use the same unit and code in a different site and never had this problem before. Tried replacing the code and the Pager class definition with those from the other site and that didn't fix it.
session_start();
Has to be called before anything is echoed to the browser, best practice is to have it as the first thing you have on your PHP page.
Giving you a better answers is no problem, if you provide more code.
More about session_start();

Session variables unset between page content changes

i am using 1 index.php page and using navigation links to send values $_GET values which are used to retrieve page content from mysql database to effectively change the page which is working fine.
On one of the nav links a session variable is set when clicked which i want to use later, but when i click another nav link that doesnt set sessions the previously set session has been unset and so has no value. i have checked the session_id which is the same on each page and have checked that the session variable is actually being set by outputing to the screen, it just disappears when a new page is clicked.
Thanks
I think (but someone will correct if I am wrong) that you should start the session all over you website for the session to be kept in place. Try to add session_start(); at the top of all your pages and see if things improve. $_GET is not related to the session so it is unclear to me why you mention it...

Using PHP variables on multiple pages

Is it possible to use a variable from one page in a piece of code on another? e.g. submit a form on one page and on the second page use a PHP script to add the data from the form to a MySQL table
Thanks for all the help
Your best bet would be to use PHP's session functions. That way if the user navigates away from your page and then comes back, the session variables will still be there (provided the session hasn't expired). You can get more information here -- PHP Sessions. Basically all you have to do is call
session_start();
at the top of each php page (before anything is outputted to the browser) where you want to have access to the session variables. You can then set/retrieve a variable using
// set
$_SESSION['varname'] = "something";
// retrieve
$somevar = $_SESSION['varname'];
This is what the GET and POST 'super' globals are for.
http://www.tizag.com/phpT/postget.php
The script that generates the form does not have to be the script the processes the form. all it takes is for the FORM tag to point to the correct script. The only caveat is that whatever page you submit the form data to also has to then generate some suitable output (unless you do a AJAX POST).
If you're instead asking if two page executions can handle the same POSTed data, well that is quite a different question. At a simplistic level, you can have the first include() the second so it has access to all the same data. A bit more advanced is to re-create the original POST and synthesize a POST. This will probably require some JavaScript assistance. Another approach is to use PHP's session feature and put the value aside so a later script has access to it. This relies on the user then following the correct link, however!
Use session to access your variable to the second/other page.
Example:
index.php
<?php
session_start();
$_SESSION['yourData'] = 'yourData';
?>
=========================================================================
second_page.php
<?php
echo $_SESSION['yourData'];
?>
You may try this one. Visit this link for more details. Or PHP's official website.

Categories