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 7985023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:27:14+00:00 2026-06-04T11:27:14+00:00

I am working on a multilingual site so I tried this approach: echo $_COOKIE[lg];

  • 0

I am working on a multilingual site so I tried this approach:

echo $_COOKIE["lg"];
if (!isset($_COOKIE["lg"]))
    setcookie("lg", "ro");
echo $_COOKIE["lg"];

The idea is that if the client doesn’t have an lg cookie (it is, therefore, the first time they’ve visited this site) then set a cookie lg = ro for that user.

Everything works fine except that if I enter this page for the first time, the first and second echo return nothing. Only if I refresh the page is the cookie set and then both echo print the “ro” string I am expecting.

How can I set this cookie in order to see its value from the second echo on the first visit/page load of the user? Should be without needing to refresh the page or create a redirect.

  • 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-04T11:27:16+00:00Added an answer on June 4, 2026 at 11:27 am

    Answer

    You can’t according to the PHP manual:

    Once the cookies have been set, they can be accessed on the next page
    load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

    This is because cookies are sent in response headers to the browser and the browser must then send them back with the next request. This is why they are only available on the second page load.

    Work around

    But you can work around it by also setting $_COOKIE when you call setcookie():

    if(!isset($_COOKIE['lg'])) {
        setcookie('lg', 'ro');
        $_COOKIE['lg'] = 'ro';
    }
    echo $_COOKIE['lg'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on a multilingual site that needs left-to-right as well as right-to-left
me working on search in a multilingual site (currently only english and turkish). As
I am working on a CMS system tries to support multilingual natively. I have
Im working on a multilingual site using asp.net mvc, and im wondering how search
Working with C#. I have an abstract class that I use to read/write settings
I am working on multilingual site and in which i used some enum and
I'm working on a multilingual site written in asp.net using mvc 1.0. I was
I am working on Spring web application and my application is multilingual. I have
Setting up a Prestashop with multistore functionality together with multilingual support. This means that
So, I know that this isn't something that is normally a good idea for

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.