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

    cant read cookie in php


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

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

    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
    Bangalore, India
    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, and you try to access it at, it won't work by default. You have to explicitly set the path, by using the fourth parameter of setcookie like so:
    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
    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, 02: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, 05: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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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