samedi 30 janvier 2021

I can't set a php cookie

I made a php script that allows me to switch the language of my website and for that, I used a cookie called lang to store the user's language prefer but I don't Know why this cookie can't be set as I can't find it in the chrome developer tap .. Although I made this script also with session variable in case cookie isn't set, it doesn't work either, as it shifts the language whenever I change the page

this is my PHP script

<?php
  session_start();
  if(isset($_GET['lang'])) {
    $lan = $_GET['lang'];
    $_SESSION['lang'] = $lan;
    setcookie("lang", $lan, time() + (3600 * 24 * 30), '/');
  } else if (isset($_SESSION['lang'])) {
    $lan = $_SESSION['lang'];
  } else if (isset($_COOKIE['lang'])) {
    $lan = $_COOKIE['lang'];
  } else {
    $lan = 'en';
  }

  include "languages/" . $lan . ".php";
?>

I have two files in that language folder en.php and ar.php and each one contains an array with translation like below

<?php
$lang = array(
  "Give" => "Give",
  "Blood" => "Blood",
  "Register as a donator" => "Register as a donator",
  "Search for a donator" => "Search for a donator"

I include my script in all my site's pages
here is my chrome developer tap screen shot

any thoughts?
Thanks for helping in advance :)




Aucun commentaire:

Enregistrer un commentaire