Proper use of session_start() with session.cookie_domain - php

Try as I might, I cannot get my session values on the other side. My set up is:
At the top of page 1:
ini_set('session.cookie_domain','.domain.com');
session_start();
On the sub domain page:
session_start();
echo $_SESSION['myval']
;
What am I doing wrong here? All I get is an empty array.

session_start needs to be called on every page that uses session variables. not just the first one

Related

$_SESSION variables last one page load

Here's the code
<?php
print_r(session_get_cookie_params());
session_set_cookie_params(3600, "/", "mydomain.com");
When I refresh the page, the above parameters are not saved
session_start();
var_dump($_SESSION);
$_SESSION['name'] = "name";
When I refresh the page, the above variable is not saved
var_dump($_SESSION);
print_r(session_get_cookie_params());
However, both variable and parameters are correctly displayed on first page load.
?>
The first var_dump ALWAYS returns an empty array, the second successfully returns the "name" within it.
$_SESSION variables only ever last one page load.
I have tried naming the session, adding a (long) timeout, linking to a different page, but $_SESSION variables only ever last one page load.
Must this be a problem with the server?
Check with session_save_path() if it is writable.
if (!is_writable(session_save_path())) {
echo 'Session path "'.session_save_path().'" is not writable for PHP!';
}
This is from a previous post : PHP Session not Saving
if you use memcache/redis/mysql session storage backend, it can be down. Is there any errors shown? Try to enable error reporting in PHP.ini and rerun the code.
Probably you have put some html output before session_start()
and this prevents from setting the session.

Using PHP session variable to keep user logged into website

I want to assign a value to a session variable when a user logs into a website. I read that I must explicitly start a session at the top of my pages in order to do this. So I inserted:
if (!isset($_SESSION)){
session_start();
echo "started";
}
The first thing I notice is that "started" is displayed every time I reload my page. Is that expected behavior? I would assume the second time I load the page, the session should already be there, therefore "started" would not display.
Further down in my page, I have:
$_SESSION['id']=2;
echo "<p>Your session ID is: " . $_SESSION['id'] . "</p>";
That correctly displays the ID in the "echo" statement. So even after assigning a value to a session variable, when I reload the page, it puts "started" at the top.
Am I doing something wrong? Thank you!
That is expected behaviour, you need to call session_start() before any output is sent to the browser every time the page is loaded, which is why your echo is happening every time. You if statement in this case is a little unnecessary and you should just simply call it without the if.
session_start();
For example, a $_SESSION variable will never be accessible unless you call session_start(), despite the fact that it will exist in the browser's session. Calling session_start() simply allows you to access that superglobal array.
You need to put your session_start() at the top of the file.
Otherwise you won't be able to use the $_SESSION array.

How to set unset session variable back?

I'm trying to test my php script, so I need to (at least temporarily) get back some of the unset variables.
I mean:
page1.php:
<?php
session_start();
$_SESSION['var']="somevalue";
go_to("./page2.php");
?>
page2.php:
<?php
session_start();
use($_SESSION['var']);
unset($_SESSION['var']);
?>
Now when I go to page1->page2, then page1 again (then page2 again) page2 does not recognize $_SESSION['var'], so I need something like
if(!isset($_SESSION['var']))
{
set_back($_SESSION['var']); //But how ?
}
in page1.php.
Any help would be appreciated. Thanks!
I think that your problem is the local browser cache.
When you go back to page1 the response is served from your computer cache instead from the server so the session variable is not set again
There is no functionality in php that "rolls back" a session/variable that has been unset (Of course you could store the original value in temporarily session like Boaz suggested). What exactly are you trying to achieve?

passing session id via url

I'm trying to get my script to use url session id instead of cookies.
The following page is not picking up the variable in the url as the session id.
I must be missing something.
First page http://www.website.com/start.php
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
$session_id = session_id();
header("location: target.php?session_id=". $session_id );
Following page - http://www.website.com/target.php?session_id=rj3ids98dhpa0mcf3jc89mq1t0
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
print_r($_SESSION);
print(session_id())
Result is a different session id and the session is blank.
Array ( [debug] => no ) pt1t38347bs6jc9ruv2ecpv7o2
be careful when using the url to pass session ids, that could lead to session hijacking via the referer!
It looks like you just need to call session_start() on the second page.
From the docs:
session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.
EDIT:
That said, you could also try manually grabbing the session id from the query string. On the second page you'd need to do something like:
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_id($_GET['session_id']);
print_r($_SESSION);
print(session_id());
Note that the session_id() function will set the id if you pass it the id as a parameter.
Instead of hardcoding 'PHPSESSID', use this:
session_id($_GET[session_name()]);
My issue was using Flash in FF (as flash piggy backs IE, so sessions are not shared between the flash object and firefox)
Using php 5.3 all these answers pointed at the truth. What I finally found to work was pretty simple.. pass the id in the query string. Set it. THEN start the session.
session_id($_GET['PHPSESSID']);
session_start();
Just a little correction ...
Don't forget to check param if it exists.
This worked for me well.
if (isset($_GET['PHPSESSID'])) {
session_id($_GET['PHPSESSID']);
}
session_start();

php session help

Ok so my main page has a session. I am using the session id to query the database.
Now when I post to a page I have session_start() at the top but when I echo the session_id() on the main page and on the page that was posted to they are 2 different values. Why is it changing the session_id()?
Is there a way to make it keep the original value?
EDIT:
index.php
<?php
session_start();
echo session_id();
?>
<form method="post" action="post.php">
<input type="text" name="some_field" />
</form>
post.php
session_start();
echo session_id();
The session_id in index.php is different from the one in post.php!
are you sure your browser is accepting cookies like it should be ? To make sure you try in a different browser then your usual one
sounds like you are some how changing the name of the session, or you need to change the name of teh session.
why not output the name of teh session as well as its ID
eg:
echo session_name();
echo "<br/>";
echo session_id();
if some hwere your session name is being changed, you will have to use that name all the time. PHPSESSID is teh default name for the session. SESSf9a1dfbab4f36b89eaf8b74ce485ad62 sounds like something you have changed it too..
you could also check session_id() to see what value is being stiored in the name. you can explicitly set all of tehse values, and if something in your code is doing so, you had better track it down.
(I am asuming that there is more to your code than what you put up)
http://www.php.net/manual/en/function.session-id.php says:
Note: When using session cookies, specifying an id for session_id()
will always send a new cookie when
session_start() is called, regardless
if the current session id is identical
to the one being set.
and
If id is specified, it will replace
the current session id. session_id()
needs to be called before
session_start() for that purpose.
Depending on the session handler, not
all characters are allowed within the
session id.

Categories