Unwanted independent sessions for two separate pages - php

I am stumped. Everybody says if you use session_start that you should get a single session even when you open multiple browser windows. However, it seems that my sessions are page specific.
When I load page one (index.php), it gives as a Session ID: sr51j9hhrjjrvbrfboek15l4e4 with an empty $_SESSION array
I then log in on a webpage (login.php) that generates a new Session ID: v2t8844nglg7uvnsrbr6k9ms43 with a $_SESSION array with various variables.
When I then reload page one, it will display the old session ID, not the new one as expected. It is page linked because if I copy load page two in tab one, it will give the same session ID as the page in tab 2, and visa versa.
I have added the rule
CacheDisable /local_files
to the httpd.conf file with no effect.
I uses Apache 2.4 (XAMPP installation for windows), with the http://php-login.net advanced login script.
Top of page (before the HTML tag):
session_start();
require_once('includes/connection.php');
followed by:
echo var_dump($_SESSION);
echo session_save_path();
echo 'Session ID: '.session_id();
at the top of the page in the body tag.
Any suggestions?

session_start() will create a single session that can be used across the same browser/tabs.

Okay, found the answer here: My session ID's stopped working
One page was localhost:8001, while the second was 127.0.0.1:8001, which are treated by the server as two separate pages for the purpose of sessions, while serving the same page.

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.

How to use session variables in wordpress

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.

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 - Values Randomly NOT Being Written

I am building a site that combines ajax and PHP to create dynamic forms that load/validate/submit in-page. I am using a combination of PHP session variables with custom handlers, $.post, and $().load to accomplish this. I build my forms in individual php pages (they are very large) and then load them into a section of my main page.
At the top of my main page I start a session like so:
<?php
require("inc/SecureSession.php"); // Custom handlers
session_save_path($_SERVER['TEMP']); session_start();
?>
Then in each form page I start a session and generate a token variable, like so:
<?php
require("inc/SecureSession.php"); // Custom handlers
session_save_path($_SERVER['TEMP']); session_start();
$_SESSION['token'] = rand();
?>
This token variable gets inserted as a hidden field at the bottom of the form page, and should write to the session.
However, I've found that session variables set in dynamically loaded (or posted) pages do not seem to consistently stay set. They work within the scope of the form page but then do not always carry over back to the main page. I thought this might be because they were not actually accessing the right session, but it seems all session variables outside the form page can be accessed within the form page.
So I am stumped. The PHP handler I am using is the one here: http://www.zimuel.it/en/encrypt-php-session-data/
Edit: When I load one of the form-pages directly, it writes 100% of the time. It is only when I load a form via jQuery that it gets wonky.
Edit2: The issue wasn't that I was declaring the code in the wrong order. I did some research and found that I am supposed to declare the save path and handler functions before I start the session. The issue appears to have been either that I opened php with "

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