Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6810385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:11:58+00:00 2026-05-26T20:11:58+00:00

I am trying to setup a session management with cookies in PHP. My code

  • 0

I am trying to setup a session management with cookies in PHP.
My code is as follows:

  if(empty($_COOKIE )) {
    setcookie('session_id', md5(uniqid()), time()+(EXPIRE CONSTANT));
  }
  $session_id = isset($_COOKIE['session_id']) ? $_COOKIE['session_id'] : 0;

I will then check session_id for 0 and print an error message if cookies are disabled.
This works fine if cookies are really disabled.

The problem is, if a user clears his history the first time he visits
the site he will get the error message even if cookies are enabled.

Anyone have any clues about this ?
Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:11:59+00:00Added an answer on May 26, 2026 at 8:11 pm

    When you do the setcookie call, the cookies will be sent when the header is output to the browser. This means the cookie won’t be available until the next page load (when the client sends the cookie back to the server). This is mentioned in the php manual for setcookie http://php.net/manual/en/function.setcookie.php:

    setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

    Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.

    You won’t be able to determine if cookies are enabled/disabled until the page has reloaded (from php). I think you’ll have to do this check with javascript, or to stay in php do a redirect after setting the cookie for the first time, something like:

        if(empty($_COOKIE)) {
                if (isset($_GET['cookieset'])) {
                        // do error message, cookie should be set
                }
                setcookie('session_id', md5(uniqid()), time()+(EXPIRE CONSTANT));
                header('location: http://mysite.com/index.php?cookieset=1');
                exit;
        }
        $session_id = isset($_COOKIE['session_id']) ? $_COOKIE['session_id'] : 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to setup an Apache/PHP/Postgresql server locally on my machine. I'm using Windows
I'm trying to setup an Active Record web session per request in my WCF
When trying to setup a lock (pessimistic), with the following code: em.lock(controlnumbers, LockModeType.WRITE); em.refresh(controlnumbers);
I am currently trying to setup Xdebug on an XP computer with Eclipse PHP
I'm trying to setup a session in Active Record for each WCF request. Here
Im trying to set up a session and i am getting a header error,
I'm trying setup a subset of boost and get it properly compiled using bjam,
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to setup a CodeIgniter based project for local development (LAMP stack), and once
After trying to setup my site for Google Webmaster Tools I found that my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.