Jump to content

PHP setcookie doesn't work?


Josh
 Share

Recommended Posts

I'm trying to remeber the user's language with a cookie in PHP:

<?php

//Set language and remember with cookie

$lang = $_GET['lang'];

if ($lang==null)

{

$lang = $_COOKIE["lang"];

if ($lang==null) $lang = "lua";

}

setcookie("lang",$lang,time()+30*24*60*60);

?><!DOCTYPE html>

<html>

<head>

<title>Leadwerks Game Engine Documentation</title>

 

Here is a URL with the language specified to CPP:

http://www.leadwerks.com/learn.php?page=API-Reference_Object_Entity_SetPosition〈=cpp'>http://www.leadwerks.com/learn.php?page=API-Reference_Object_Entity_SetPosition〈=cpp

 

This URL does not specify any language, and will default to LUA:

http://www.leadwerks.com/learn.php?page=API-Reference_Object_Entity_SetPosition

 

However, if you click the first link then the second link, a cookie should remember your language selection and display CPP the second time. Why is this not working?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I'm not getting a request to set a cookie when I visit that page. When a server wants to add a cookie to a users browser, the server will send a "Set-Cookie" header.

 

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

 

A simple solution and one that I prefer is to use $_SESSION instead of cookie. That way the data is stored on the server instead of on the users browsers making it more secure.

 

Make sure to call session_start() when working with $_SESSION.

Link to comment
Share on other sites

I got it working! You have to click on the Lua / CPP links in the documentation page, but it works. So if you just select C++ once, it will stay that way next time you open a page.

 

One step closer. :)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

http://www.leadwerks.com/werkspace/topic/15851-documentation-xml-raw-data/#entry105733

 

Btw. I copied the PHP code you posted above and tried it locally and it worked...

How does asking a question about cookies equal I dislike something you made?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...