Wordpress - SESSION variable does not last - php

I've spent some time researching answers on this problem, but neither have resulted in fixing my issue. I attempted to make use of the $_SESSION variable. On the first page, I put the result of a input form entry (a name) into it, as it will be used across a variable number of pages. On the page after, the session variable seems to be working fine. I used a var_dump to debug it and everything seems to be set well. Then I click the link I created to go to the next page in this chain of linking. I assume that because the session is being saved, the variable will last until the user exits the website or browser.
Suddenly, on this second page, the session variable becomes null again.
I did a temporary fix where I just pass the GET value from the first page and plug it into the link page links url through some php. It works as intended, but will love to be able to make sessions work when I get to more complicated problems.

Related

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();

How to make a url parameter stick in php

Okay php newbie here, please bear with me. I am not sure if this is a redundant question but here goes. I have a reference code i want to stick to my url. example: site.com/index.php?refcode=123. That's fine right? we can put anything on there. Naturally the visitor goes to the index page. But if the visitor then clicks on other buttons that leads to other pages in my site, the parameter is gone. Like I want to track which code the visitor has when he sends me an email when he later decides to go to my contact page. How can this be done with php? or can this be done with jquery?
You would be best off saving the url variable into a session variable instead. The session variable will stick with the user so you have access to it no matter what page they go to.
$_SESSION['refcode'] = $_GET['refcode'];
Make sure to use session_start()
But if you do want to do it the way you have asked you can modify all urls on your page and add:
'?'.$_SERVER['QUERY_STRING']
This will add the query string to your url so the next page they go to would still have it. But that does seem like a lot more work.

PHP SESSION variable get's deleted each reload

Does anybody know what can cause the $_SESSION variable to be cleared?
The session variable is used to keep track of products in the cart.
Everything worked perfect on the development and production server.
Out of a sudden the production server looses the session variable - without any changes updated. The production server is hosted with 1&1.
I added some debug information which prints the contents of GET, POST and SESSION.
Link to the website:
http://niehues-gmbh.de
Any cart button updates the SESSION variable
But any reload and other links delete the SESSION
I do call session_start() at the beginning of index.php which loads all other contents.
Be careful of tabs. If someone loads product detail in another tab, the browser sees it as a reload or link, and as you said, session is cleared at these times. It's better to store your cart in the database.
I fixed the issue by keeping the HTML / PHP standard.
If I remember right then there were white spaces before a header function which made the page not work correctly.

Browser goes Back after PHP page is executed

I have a form which sends data to a proccess page. Is it possible when the code in the process page is executed for the browser to jump back to the previous page?
EDIT
The reason i want this, is because i have a set of parameters which contains checkboxes. These parameters are echoed out via a while loop. I have a table where it shows which of those parameters are active. I would like to check the checkboxes by defualt where the corresponding parameter is in the table.
|||||||||||||||||||||||||||||||| Example ||||||||||||||||||||||||||||||
PARAMETERS:
T-Shirt: checked
Distance: checked
Race: unchecked
TABLE (parameters)
• T-Shirt
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||the example above checks the box if it already exists in the parameters table. When the user clicks back after the process page, the boxes he selected is checked.
Hope i am clear enough
Regards
I think a nice solution might be to implement a front controller for your webbage, a central script to include the appropirate page. Using a system like that, the user stays on the same page all the time even when other pages are loaded.
I haven't used a front controller in quite some time, so I won't write examples due to potential errors and poor coding. You can probably find some examples using google though, I wouldn't be surprised if the subject has been brought up here on Stackoverflow before either.
Even though I'll have to point out Griffin's solution is the best, if you're dead set on a redirect, simply have your PHP script execute the following lines:
echo '<script>document.history.go(-1);</script>';
die();
Or even
die('<script>document.history.go(-1);</script>');
It's a dirty solution, so I must advise against it.

Categories