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

  • Home
  • SEARCH
  • 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 8512471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:17:20+00:00 2026-06-11T04:17:20+00:00

I am trying to set cookies to a php session variable so that a

  • 0

I am trying to set cookies to a php session variable so that a session does not expire for 2 months. The problem I am getting though is that after about 3 hours, when I refresh the page, I get a undefined index for $_SESSION['id'] in line 30. But I don’t get the undefined index for $_SESSION['id'] in between lines 20-23.

Why am I still getting undefined indexes even though I have set cookies for this session variable?

$inTwoMonths = 60 * 60 * 24 * 60 + time();
    setcookie('id', 'ID', $inTwoMonths);


    //line 20-23
    if (isset($_POST['id'])) {

    $_SESSION['id'] = $_POST['id'];

    }

    //line 30
    <?php echo $_SESSION['id'] ?>
  • 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-06-11T04:17:21+00:00Added an answer on June 11, 2026 at 4:17 am

    SESSION != COOKIE.

    You must set SESSION TTL in php.ini (20min default).

    On line 20-23 you dont get undefined index, coz you setting var. But on line 30 you trying to read undefined variable(coz you dont set-up it before.)

    Try something like this:

    $_SESSION['id'] = $_COOKIE['id'];
    

    Also you need use session_start(); to init session.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to setup a session management with cookies in PHP. My code
I'm trying to logout of my page but session_destroy and setting cookies does not
Im trying to set up a session and i am getting a header error,
I am getting an error while trying to set these cookies through a foreach
I'm new to cookies, and im trying to set a cookie where to store
I'm trying to find a way to set a cookie that sets a user's
I have run into a very interesting problem trying to debug my custom PHP
I'm trying to set two cookies for the same site. (Except one is a
I'm trying set cookies for dynamic features. I've been able to get the on
How do I reopen a session in PHP without getting header already sent warnings?

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.