cant read cookie in php


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: cant read cookie in php

  1. #1
    Join Date
    Dec 2008
    Posts
    13

    cant read cookie in php

    Hi,

    I'm having trouble reading my cookies. Here's what i use to set cookies.

    Code:
    setcookie("usercookie", $useridi, time()+3600);
    and here's what i use to read it on another page on the same domain

    Code:
    echo $_COOKIE["usercookie"];
    But the echo does not work from another page. Surprisingly if i echo the cookie on the same page it works fine, its only if i try and read the cookie on a separate page that it echo's a blank. Please help me out.

  2. #2
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    By default, a cookie is applicable only to pages that are within the directory of the page that sets the cookie. So, if your page that sets the cookie is at http://www.example.com/path/page.php, and you try to access it at http://www.example.com/anotherpath/anotherpage.php, it won't work by default. You have to explicitly set the path, by using the fourth parameter of setcookie like so:
    Code:
    setcookie("usercookie", $useridi, time()+3600, "/");
    This will set the path of the cookie as "/". If it is in a different subdomain, then you will also need to provide the fifth argument.

    By the way, it looks like you are using cookie to manage user sessions. There are better and safer ways to do it, like using PHP Sessions.

  3. #3
    Join Date
    Dec 2008
    Posts
    13
    thanks a lot for the tip razee

Similar Threads

  1. Replies: 1
    Last Post: 01-11-2007, 04:14 AM
  2. Delete cookie not working (PHP)
    By Wildcatbob in forum Web
    Replies: 5
    Last Post: 06-23-2006, 03:24 PM
  3. How to read objects from file in a loop
    By mail2vinaybabu in forum Java
    Replies: 10
    Last Post: 02-27-2005, 02:07 PM
  4. Why can my other ASPages read a cookie file
    By kenhow in forum ASP.NET
    Replies: 1
    Last Post: 12-06-2001, 06:02 PM
  5. httpcookie problem
    By AAnthony in forum .NET
    Replies: 0
    Last Post: 06-11-2001, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center