How to use session variables in wordpress - php

I have a big problem with Wordpress and can't find a solution.
I'm trying to make a value available from every *.php file.
I want with $_SESSION['session_id'] = $var; but I've read that in Wordpress Sessions are not available.
I've tried it but it looks like that my Session is only saved once and after page reload the value is gone.
On my second page I get the Session with $var = $_SESSION['session_id'];
I've tried it with session_start() in my init hook and on my pages too where I use the Sessions but no effect. Is there someone who has an idea who I can transmit my values from one page to another without cookies because I don't want the users to see the values.

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

Wordpress - SESSION variable does not last

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.

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...

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.

Categories