Solved

Cookies are not found when page without www prefix is loaded

Posted on 2004-08-19
6
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

726 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