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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:43:19+00:00 2026-05-26T06:43:19+00:00

I’m trying to set up, what I thought would be, a simple language switch.

  • 0

I’m trying to set up, what I thought would be, a simple language switch. I thought I’d use PHP cookies, but they’re not behaving as intended.

I’ve read a few cookie tutorials and looked at a few similar examples here on StackOverflow, but I must be missing something because it can’t get it to work properly.

I’m setting the language by passing it as a URL variable (lang=en or lang=ru). That all seems to be fine. However, the code I have at the moment that sets the cookie seems to be one step behind, so initially it has no value (I’d like it to be ‘en’ by default), then if the user clicks the ‘ENG’ button it still has no value, and then if the user clicks Russian the value shows as ‘en’, and then if I click the ‘ENG’ button again the value shows as ‘ru’.

Here’s the code I’ve cobbled together:

if( $_GET['lang'] ) {
    $lang = (string)$_GET['lang'];
    setcookie( 'lang', $lang, time() + 60*60*24*30 );
} elseif( !isset($_COOKIE['lang']) ) {
    $lang = 'en';
} else {
    $lang = $_COOKIE['lang'];
}

Once I’ve got this working I intend to use the value of the cookie to display either the English or Russian menu using a bit of conditional PHP.

Thanks.

  • 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-26T06:43:19+00:00Added an answer on May 26, 2026 at 6:43 am

    Thanks for all the suggestions – @Mob set me in the right direction, i.e. processing the cookie on another page and then sending you back to the first.

    I did a bit more thinking and experimenting and I’ve finally solved it. I’ll post the code below incase anyone else wants to use this.

    On your main page put this:

    <form action="language_switcher.php" method="post">
        <select name="lang">
            <option value="en"<?php if( $_COOKIE["language"] == "en" ) { echo " selected"; } ?>>English</option>
            <option value="ru"<?php if( $_COOKIE["language"] == "ru" ) { echo " selected"; } ?>>Russian</option>
        </select>
        <input type="submit" value="Select Language">
    </form>
    
    <p>Language: <?php if( isset( $_COOKIE["language"] ) ) { echo $_COOKIE["language"]; } else { echo "<em>not set</em>"; } ?></p>
    

    Then in another file called ‘language_switcher.php’ put the following code:

    $lang = "en";
    if( isset( $_POST["lang"] ) ) {
        $lang = $_POST["lang"];
        setcookie ( 'language', $lang, time() + 60*60*24*30, '/', 'mydomain.com');
        header( "Location: /previous_page_url.php" );
    }
    

    The user chooses a language and clicks ‘Select Language’. The form then sends the form value to ‘language_switcher.php’, which sets the cookie and then sends the user back to the previous page.

    Done! 🙂

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.