Solved

php session variable disappears

Posted on 2010-11-25
7
623 Views
Last Modified: 2012-05-10
Hello,

Please help to find solution to the next issue:

In cms administration (somesite.com/admin) session variable with some value ($_SESSION[’admin_id’]) is created for the user. When after administration go to the main website page and back to administration, the session variable disappears $_SESSION[’admin_id’].
The main page has its own session variables with values and variable $_SESSION[’admin_id’] is not used there.

The issue has to be with my hosting, since on other server everything is fine. What should I check on my server?

Server details: CentOS, PHP 5.2.14,
Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
PHP 5 Handler - SuPHP
Apache suEXEC "on"
Apache modules:
Loaded Modules:
 core_module (static)
 authn_file_module (static)
 authn_default_module (static)
 authz_host_module (static)
 authz_groupfile_module (static)
 authz_user_module (static)
 authz_default_module (static)
 auth_basic_module (static)
 include_module (static)
 filter_module (static)
 deflate_module (static)
 log_config_module (static)
 logio_module (static)
 env_module (static)
 expires_module (static)
 headers_module (static)
 unique_id_module (static)
 setenvif_module (static)
 version_module (static)
 proxy_module (static)
 proxy_connect_module (static)
 proxy_ftp_module (static)
 proxy_http_module (static)
 proxy_scgi_module (static)
 proxy_ajp_module (static)
 proxy_balancer_module (static)
 ssl_module (static)
 mpm_prefork_module (static)
 http_module (static)
 mime_module (static)
 status_module (static)
 autoindex_module (static)
 asis_module (static)
 info_module (static)
 suexec_module (static)
 cgi_module (static)
 negotiation_module (static)
 dir_module (static)
 actions_module (static)
 userdir_module (static)
 alias_module (static)
 rewrite_module (static)
 so_module (static)
 bwlimited_module (shared)
 bw_module (shared)
 suphp_module (shared)
 security2_module (shared)

Any help will be highly appreciated.
0
Comment
Question by:tanel
  • 4
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34212958
Check your PHP configurations to see if there is a setting to "auto-start" the PHP session.  It may be on in one configuration and not on in another configuration.  Whether or not you find that to be the case, you may have a logic error in your session handling.  You need to have a session_start() statement on every page right at the top.

0
 
LVL 2

Expert Comment

by:schwomp
ID: 34213737
Hello.

Simple question : are the website and the administration exactly on the same domain?

Depending on settings, cookies are linked to a precise domain. I had a similar problem with a site that was working with and without the "www".

Bye.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34213891
@schwomp:  That may be a good point.  By default the PHP session handler sets cookies for ONLY THE SUBDOMAIN that is called.  Here is how to set session cookies that will work across http://domain.com and http://www.domain.com (as well as others).
<?php // RAY_session_cookie_domain.php

/* *

 * QUESTION: WHEN CLIENTS VISIT MY SITE SOMETIMES THEY USE www.mysite.org

 * BUT SOMETIMES THEY USE mysite.org WITHOUT THE WWW.  HOW CAN I HANDLE

 * THE SESSION ISSUES THAT ARISE FROM THIS?

 *

 * ANSWER: ONE WAY IS TO REWRITE THE URL TO REMOVE THE SUBDOMAIN IF IT

 * IS WWW.  FOR EXAMPLE:

 *

 *     Options +FollowSymlinks

 *     RewriteEngine on

 *     RewriteCond %{http_host} ^www\.example\.org [NC]

 *     RewriteRule ^(.*)$ http://example.org/$1 [R=301,NC]

 *

 * ANOTHER WAY IS TO MODIFY THE SESSION COOKIE SO IT WORKS ACROSS ALL OF

 * YOUR SUBDOMAINS.  YOUR CHOICE WILL LARGELY DEPEND ON THE WAY YOU WANT

 * TO HANDLE OTHER SUBDOMAINS (OTHER THAN WWW).

 */



// DEMONSTRATE HOW TO START SESSIONS THAT WORK IN DIFFERENT SUBDOMAINS PHP 5.2+

error_reporting(E_ALL);





// MAKE THE SESSION COOKIE AVAILABLE TO ALL SUBDOMAINS

// MAKE A DOMAIN NAME THAT OMITS WWW OR OTHER SUBDOMAINS

// BREAK THE HOST NAME APART AT THE DOTS

$x = explode('.', strtolower($_SERVER["HTTP_HOST"]));

$y = count($x);

// POSSIBLY 'localhost'

if ($y == 1)

{

    $host = $x[0];

}

// MAYBE SOMETHING LIKE 'www2.atf70.whitehouse.gov'

else

{

    // USE A DOT PLUS THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN NAME

    $host = '.' . $x[$y-2] . '.' . $x[$y-1];

}



// START THE SESSION AND SET THE COOKIE FOR ALL SUBDOMAINS

$sess_name = session_name();

if (session_start())

{

    // MAN PAGE http://us.php.net/manual/en/function.setcookie.php

    setcookie($sess_name, session_id(), NULL, '/', $host, FALSE, TRUE);

}





// PROVE THAT THE COOKIE WORKS IN MULTIPLE DOMAINS

// LOAD UP SOME INFORMATION TO SHOW SESSION CONTENTS

$_SESSION["cheese"] = "Cheddar";

if (!isset($_SESSION["count"])) $_SESSION["count"] = 0;

$_SESSION["count"] ++;





// PUT UP TWO LINKS WITH DIFFERENT SUBDOMAINS

// STRIP OFF THE DOT THAT WAS NEEDED FOR SETCOOKIE

$gost = ltrim($host,'.');

$dmn_link = 'http://'    . $gost . '/RAY_dump_session.php'; // var_dump() SCRIPT

$www_link = 'http://www' . $host . '/RAY_dump_session.php';



echo "<br/><a target=\"_blank\" href=\"$www_link\">$www_link</a>" . PHP_EOL;

echo "<br/><a target=\"_blank\" href=\"$dmn_link\">$dmn_link</a>" . PHP_EOL;





// SHOW WHAT IS IN COOKIE AND IN $_SESSION

echo "<pre>";

echo "COOKIE ";

var_dump($_COOKIE);

echo PHP_EOL . PHP_EOL;

echo "SESSION ";

var_dump($_SESSION);

echo "</pre>";





?>

<form method="post">

<input type="submit" value="CLICK ME" />

</form>

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:schwomp
ID: 34213936
Setting session.cookie_domain might be simpler :

http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain

Bye.
0
 

Author Comment

by:tanel
ID: 34215996
session.auto_start is off on the both servers and there is session_start() on every page.

The website and the administration are on the same domain and the site works with / without www on both servers.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34217813
Suggest you add var_dump($_SESSION) to every page.  You can do this inside HTML comments and use "view source" to see the contents of the session.  It sounds like a logic error of some sort, but without seeing the code and the web pages in action all we can do is suggest debugging ideas.

If you want to post the code and a link to your test case, I'll be glad to try to follow the logic.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34230570
Thanks for the points - I'm glad it's working now.  Best, ~Ray
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now