Solved

Cookies are not found when page without www prefix is loaded

Posted on 2004-08-19
6
305 Views
Last Modified: 2012-06-27
I just noticed a big problem with my site.

When a user visits my page, www.<something>.com and signs in, I have coded the cookies so that the path is "/".  So the cookies work fine through all the pages. But, there's a section of my site that switches to just <something>.com (www is removed) so now the cookies are not even there cause they belong to www.<something>.com.

I have no idea why that particular section switches but I need to figure out how to assign the cookies to both www. and without if that's even possible.

 I don't know if there's a better/different solution so please let me know.

Thanks.

------------------------------------------------------------

code snippet of signin.php
~~~~~~~~~~~~~~~~~

if(isset($_REQUEST["chk_remember"]) == "yes"){
     $cookie_life = time() + 31536000;
}else{
     $cookie_life = 0;
}
setcookie('ows_userID', $var_userID, $cookie_life, '/');                  
setcookie('ows_password', $var_password, $cookie_life, '/');                  
setcookie('ows_validated', 'yes', 0, '/');                  
header("Location: signin2.php");
0
Comment
Question by:bloodtrain
  • 3
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 100 total points
ID: 11844611
you need to include the domain parameter in the setcookie function and specify it as

.domain.com

(notice the initial dot) so it is available across all subdomains
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11844613
$domain = '<something>.com';

setcookie('ows_userID', $var_userID, $cookie_life, '/', $domain);              
setcookie('ows_password', $var_password, $cookie_life, '/', $domain);              
setcookie('ows_validated', 'yes', 0, '/', $domain);              

So the cookiea are available even if the uses uses www or not

see http://de3.php.net/manual/en/function.setcookie.php for detailed option to set via setcookie
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11844615
ok, well, I realized why that particular section of mine was losing the www prefix so my problem is solved. But I would still like to know if you can share the same cookie between www and no www..  

Thanks guys / gals!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:Diablo84
ID: 11844626
reference: http://www.php.net/manual/en/function.setcookie.php

and quote regarding my above comment:

 To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com  will make the cookie only available in the www  subdomain. Refer to tail matching in the spec for details.

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11844642
example

setcookie('ows_userID', $var_userID, $cookie_life, '/','.yourdomain.com');        
0
 
LVL 2

Author Comment

by:bloodtrain
ID: 11844645
You guys are too quick..

Diabo84, I will try you method first since you did post first..


Thanks both of you..
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

706 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

20 Experts available now in Live!

Get 1:1 Help Now