Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2013, 06:06 AM   PM User | #1
holy24
New Coder

 
Join Date: Dec 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
holy24 is an unknown quantity at this point
Problem using $_SESSION

Hi,

I have been trying to use $_SESSION when a user successfully login to a website (eg. abc.com), but I am not sure why when I login another website(eg. zzz.com) simutanuously, it capture zzz.com details and show in abc.com.

Steps to reproduce:

1. Login to abc.com

2. Upon successful login, in home.php, i echo the $_SESSION["number"] . It correctly shows the staff's number (eg. E123).

---------------------------------
login.php
-------------------------------------

session_start();

$login=mysql_query("SELECT * FROM staff WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");
$row=mysql_fetch_array($login);

if (mysql_num_rows($login)==1){

$_SESSION["login"]=true;

$_SESSION["number"]=$row['number'];

header('Location: home.php');

}
------------------------------------------

-------------------------------------------
home.php
--------------------------------------

<?php
session_start();
if(!$_SESSION["login"]){
header('Location: index.php');
}

echo $_SESSION['number'];

?>
-------------------------------------

3. However, if i login to another website (eg. zzz.com) using username: E999 and I go back abc.com, the $_SESSION['number'] change from E123 to E999.

Both abc.com and zzz.com are using different database, why the $_SESSION['number'] in abc.com is capturing the info from other website?

Can anyone kindly advise on this? Thanks.
holy24 is offline   Reply With Quote
Old 01-09-2013, 09:35 AM   PM User | #2
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
Are you visiting zzz.com in thesame browser or browser session? In that case the session details will simply be overwritten, if zzz.com uses thesame session-variable ($_SESSION['number']) as abc.com.

Someone please correct me if I'm wrong, but is this solvable by storing your sessions in a local database and pulling the information from there? I've never worked with this before, so can't elaborate on the exact workings.
Thyrosis is offline   Reply With Quote
Old 01-09-2013, 12:13 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
It shouldn't make any difference Thyrosis. The session cookie should be domain specific. The browser should recognise the difference between abc.com and zzz.com and only send the cookies related to each domain.

To be honest, I don't think there is anything wrong with the code that I can see. I think there might be something else going on with the browser or the configuration.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-09-2013, 01:32 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Not necessarily, there is one other potential cause non-related to cookies.
Are you sure you are using cookies for your sessions? Are you passing a querystring in any fashion that would allow the second domain (this is a domain right? Not a subdomain which is a completely different problem) which if hosted on the same server could read the same session file?
The only time websitea.com and websiteb.com can actually change data in each other's sessions is if they are on the same server AND phpsessid is passed through the querystring to the other server. You can try changing the save path locally by setting session_save_path to a new location prior to calling session_start.
Fou-Lu is offline   Reply With Quote
Old 01-10-2013, 12:48 AM   PM User | #5
holy24
New Coder

 
Join Date: Dec 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
holy24 is an unknown quantity at this point
Hi,

Actually, as what I suspect, it might be because both website is in the same host (testing) and the session variable (number) is the same.

my 1st website:
http://testing/abc/login.php
$_SESSION['number']

my 2nd website:
http://testing/zzz/login.php
$_SESSION['number']

There is one way where I can change all the session variables to different name but it would be a problem if I have alot of web application.

Can anyone please kindly advise if there is any code where the session variable will not inter-link from different web application even though they are in the same host/same session variable name.

Thanks in advance for the help.
holy24 is offline   Reply With Quote
Old 01-10-2013, 02:25 AM   PM User | #6
holy24
New Coder

 
Join Date: Dec 2012
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
holy24 is an unknown quantity at this point
Hi,

Thanks for the advice.

I have found out 1 solution where i can use a unique session_name in different site in the same host.

config.php:
<?php
session_name('test');
session_start();
?>

at the beginning of each file:
<?php
include 'config.php';
?>
holy24 is offline   Reply With Quote
Old 01-10-2013, 02:23 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The other alternative(s) which I would recommend over the session_name is changing the session's save path (do it in a global file used prior to anything else), and using a database instead. Both of these eliminate the possibility of conflict, assuming they are both configured differently.
Lately I've learned more about the sessions when using the save handler. Its definitely easier to use than my old manual db sessions and a lot less code overall, but I had to write the encoder and decoders for the serialized data since PHP doesn't really have a built in way of doing it (and I don't pull from the session superglobal itself). So that did take a bit of work. If you don't need to split up the data, than that won't be necessary, just a blob type would do.
Fou-Lu is offline   Reply With Quote
Old 01-10-2013, 04:48 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by holy24 View Post
I have found out 1 solution where i can use a unique session_name in different site in the same host.
That shouldn't really have affected it though in the first place. When you call session_start, it should generate it's own random identifier and (assuming you're using the default cookies to store it) should only be used on a per-domain basis.

I still think there is something else here that is playing up.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-10-2013, 05:15 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by tangoforce View Post
I still think there is something else here that is playing up.
Yep, there sure is. If its actually going across domains, the only way to pass the sid is via the querystring. So if you check the HTML links you may find that the sid is being passed across domains which should be fixed immediately.
Given the one post here though, I question if we are actually looking at separate domains. There is indication that its simply under /abc/ and /zzz/, in which case session cookies can be modified to only adhere to the directory level in which they were set. That can be done via an ini set as well with the session.cookie_path and changing it to /specificdir prior to calling session_start(). That should work.
Fou-Lu is offline   Reply With Quote
Old 01-10-2013, 06:04 PM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Fou-Lu View Post
There is indication that its simply under /abc/ and /zzz/
You know something Fou, I think you may well be right. Thinking about it, many registrars offer domain forwarding via frames so you can point it straight at a url instead of tinkering with DNS which many folks don't understand how to use. That would certainly explain the same sessions being used with two different domains using the same domain as the main host.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 AM.


Advertisement
Log in to turn off these ads.