?
Solved

Cookies are not found when page without www prefix is loaded

Posted on 2004-08-19
6
Medium Priority
?
315 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 400 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
Independent Software Vendors: 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

752 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