i'm having a very weird session problem under php:
(works perfectly locally but not on my internet-server)
the problem:
i'm loading a page - i'm defining a php session via jQuery $.post in an external php script
i'm refreshing the page - session is still there
i'm loading a different page - the session is gone (empty)
there's no unset or anything which might reset/clear the session.
i'm using a global php include for the header which triggers session_start();
any ideas?
thanks
Try setting your cookie parameters to allow the domain to work across subdomains:
$params = session_get_cookie_params();
session_set_cookie_params($params['lifetime'], $params['path'], '.example.org');
Where example.org is your domain name.
Then before printing anything to screen start your session:
session_start();
On every page/resource you wish to be able access session data.
Related
I need to reset session each time the user goes back to index.php, in the index i got
<?php
session_start();
if(isset($_SESSION['prevail'])) session_unset();
?>
where $_SESSION['prevail'] is first session variable that is set on the next page. At the moment when I visit any page it resets session just like it was loading index.php before loading the page (with isn't possible cause I use permanent links to pages and they aren't sharing any header.
It also happens when I put session_destroy() in other files.
On the server the app is in directory public_html/app and in public_html is wordpress installed. App has it's own subdomain though. Any suggestion will be appreciated :)
I need to reset session each time the user goes back to index.php
i will use below code to do so:
index.php
<?php
session_start();
if(!empty($_SESSION['prevail'])) {
unset($_SESSION['prevail']); // only unset that session
}
Try this.. ;)
I've this little problem: PHP is not saving the cookie to my (cookie allowing) browser, other sites are fine but this one fails to save the session id in the cookie, ergo an inability to access necessary data.
The index page does a
require("includes/functions.php");
which successfully requires my functions file:
session_name('login');
// Starting the session
$expiretime = 60*60*24;
session_set_cookie_params($expiretime);
// Making the cookie live for 1 day
session_start();
However, the login cookie is not saving (checked via Firebug) and I've no reason why. Thanks for the help
Try displaying the session cookie parameters to make sure they are ok by running after session_start:
var_dump(session_get_cookie_params());
If path (or domain) doesn't match the prefix of your web app path, then you might have to set it explicitly:
session_set_cookie_params($expiretime, '/');
or
session_set_cookie_params($expiretime, '/myapp/');
Newbie question, but I'm wondering if I'm missing something elementary here.
If I register a session variable in a page - isn't this variable supposed to be accessible from another page on the same site?
First, I register a variable in the file session_var_register.php:
<?php
$_SESSION["myusername"] = 'user';
if (isset($_SESSION['myusername'])) {
echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>
When I open this page, it writes:
Session var myusername is set to user
As expected.
Then I open another tab and another page, check_session_var.php:
<?php
if (isset($_SESSION['myusername'])) {
echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>
This page is blank.
Isn't the point of a session variable that it should be accessible in the browser session, until the session is programatically destroyed or the browser closed?
I'm using IE 8 and Firefox 24, btw. Identical results.
You forgot
session_start()
On top, before using
$_SESSION
PS: Remember to call session_start() in every page you want to use $_SESSION.
The PHP docs state that you must call session_start() to start or resume a PHP session. This must be done before you try to access or use session variables. Read more here.
session_start();
Your session variables will be available on different pages of the same site but on top of each of these pages you must have at least:
session_start();
It works but not in all cases. You must also use the same session name (essentially a cookie name that stores id of your session) on all pages. Moreover cookies (which are essential (mostly) for sessions to work) may be made visible only in specific directory. So if for example you share the same host with other guys that use sessions too you do not want to see their variables and vice versa so you may want to have sth like that:
1) session_name( 'my_session_id' );
2) session_set_cookie_params( 0, '/my_dir', $_SERVER['HTTP_HOST'], false, true );
3) session_start();
You may also want to see your session variables on other servers and in such case custom session handlers may be useful. Take a day or two to implement yourself - great way to understand how sessions work hence I recommend.
Method
session_start();
Description
session_start() creates a session or resumes the current one based on a session identifier >passed via a GET or POST request, or passed via a cookie.
Usage in your case (and in the most of cases):
Put it before the $_SESSION usage.
Reference: session_start()
First Of all start session on that page
session_start();
your page like this way
<?php
session_start();
if (isset($_SESSION['myusername'])) {
echo 'Session var myusername is set to '.$_SESSION['myusername'];
}
?>
I had a login system set up that stored a session variable and checked it on each page, but then I moved to a new server.
Now any session variable I set is only available on the page it was set on. I've been searching for reasons why this could happen, and already crossed off permissions issues. Is it possible this has to do with incorrect urls? Everything else on the server appears to be working fine.
I'm running the latest version of PHP and Apache if that helps at all.
Because you probably (just assumption) have not got session_start(); throughout your other pages where required. So for example, create a page called session.php
Session.php
session_start();
if (!isset($_SESSION))
{
// Enforce logout as session is not set.
}
then:
include "session.php";
use this snippet through out your pages where your login features are required.
I've run into issues like this before. You might try setting a session id when you first start the session using session_id(), and then use the same session id before each session_start().
For example:
<?php
session_id(integer);
session_start();
?>
I have a login page than involved destroying a session and starting a new one. I have very inconsistent results between both Chrome and Firefox.
I am clearing the session using:
session_unset();
session_destroy();
session_start();
$_SESSION = array();
But variables in the session seem to still exist until I refresh the page and then they disappear. My second problem ontop of this is that crucial $_SESSION variables are different on ajax pages called from this login page. This is causing big problems and inconsistent results on ajax pages.
What is the best way to destroy a session and set it with fresh variables that will be available to ajax pages?
If you're using session cookies you have to "remove" them as well.
$cookie_params = session_get_cookie_params();
setcookie(
session_name(),
false,
strtotime('2000-01-01')
$cookie_params['path'],
$cookie_params['domain'],
$cookie_params['secure']
);
Of course the cookie will not be deleted by the browser until you sent the response.
The new session would be created on the next request.
PS: The manual states:
Only use session_unset() for older deprecated code that does not use $_SESSION.
I found:
session_unset();
session_destroy();
session_start();
$_SESSION = array();
To be very unpredictable and yielded varying results between browsers which is unusual for PHP.
To resolve I simply replaced it with:
session_start();
$_SESSION = array();
I know this doesn't completely clear and replace a session, but all I really needed was the session to be cleared. The fact that the session has the same session_id doesn't really matter in my scenario.
Hope this helps some people having the same mare as me!