Jump to content

Entire Source Code


Guest Stefán Örvar Sigmundsson

Recommended Posts

Guest Stefán Örvarr Sigmundsson

Okay, I had a problem with variables and sessions and nobody seemed to know what was wrong so I'm including the entire source code so you can run it and see for yourself: ------------ home.php ------------ <?php session_start(); $loggedIn = $_SESSION['userID']; if ($loggedIn == null) include("language.php"); else $language = $_SESSION['lang']; ?> <html> <body> <p>Session: <?php echo $session; ?></p> <p>_SESSION: <?php echo $_SESSION['lang']; ?></p> <p>Cookie: <?php echo $cookie; ?></p> <p>_COOKIE: <?php echo $_COOKIE['lang']; ?></p> </body> </html> ------------ language.php ------------ <?php $session = $_SESSION['lang']; $cookie = $_COOKIE['lang']; $server = $_SERVER['HTTP_ACCEPT_LANGUAGE']; if ($session != null) { $language = $session; } elseif ($cookie != null) { $language = $cookie; } elseif ($server != null) { $languageArray = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]); $lang == "none"; foreach ($languageArray as $value) { if ($language != "none") { $langArray = substr($value, 0, 2); switch ($langArray) { case "de": $language = "en"; // should be "de" break; case "en": $language = "en"; break; case "es": $language = "en"; // should be "es" break; case "fr": $language = "en"; // should be "fr" break; case "is": $language = "en"; // should be "is" break; } } if ($language == "none") $language = "en"; } } else { $language = "en"; } $_SESSION['lang'] = $language; $_COOKIE['lang'] = $language; ?> ------------ expected results (with my language settings) ------------ Session: is _SESSION: is Cookie: is _COOKIE: is ------------ actual results (with my language settings) ------------ Session: _SESSION: is Cookie: is _COOKIE: is ------------ conclusion ------------ The 'lang' session never seems to be set because the session variable is always null. I asked a mate and had him read my code and he told me he'd had the same problem when he once tried to make a language selection script for his website and he was never able to resolve it. I hope I'm not doomed.

Link to comment
Share on other sites

I don( expect this to fix it, but you have no head or title elements in your page. Might as well get off on the right foot...

Link to comment
Share on other sites

Is the language.php file getting included? Have you tested for that? It looks like $session only gets set in language.php. I don't think null is the right value to test for, you might want to use the empty function instead to test.

Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

I did have the tags you mentioned chibineku but I removed them to save space here. To justsomeguy: Yes, it's being included and I've tried embedding it as well. I've tried putting something like $_SESSION['lang'] = "en"; at the top and then everything works - both with isset and null, but of course I don't want it hard-coded. I only seem to be able to set the session but not take the value of another variable and set the session to that - and even when I do that it doesn't store the session when I refresh the page! Does any of you have a server to test this on. I'd hate to bother the PHP team and tell them I have a possible bug. I've been to so many forums with this problem and nobody seems to get it. I'm considering switching to ASP.NET. I never got unexplainable errors using it.

Link to comment
Share on other sites

It looks like it's working like it should for me. When I save those two files you posted and load home.php the first time I get this:

Session:_SESSION: enCookie: _COOKIE: en

When I refresh I get this:

Session: en_SESSION: enCookie: _COOKIE: en

That looks like it's doing what's expected, that output matches what I see the code doing. One thing to keep in mind is that if you want to set a cookie, you need to use the setcookie function, just assigning a value to $_COOKIE does not actually set a cookie.

Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

Lol, on my server it does set the cookie even if I don't use setcookie, which is something I thought wouldn't work but I went with it anyways, but on the other hand the session thing does not work for me. My best guess is that there's something wrong with my host's servers. Do you know of any proper hosts. I'm with 1&1 (http://1and1.co.uk) right now. Or perhaps you've got your own server set up?

Link to comment
Share on other sites

on my server it does set the cookie even if I don't use setcookie
Are you sure about that? If you delete all cookies and then use that method, do you actually see a new cookie in the browser? It *will* set the entry in the $_COOKIE array for the current page, but it won't be there the next time they visit, it won't actually send a new cookie to the browser that the browser will send back to the server. That's why the $cookie variable in your script is empty.1&1 is supposed to be a pretty decent host, I am running this on my own server though. Frankly I haven't heard of any servers having problems with sessions though. You might see some problems if you're testing on a local server that doesn't have a proper domain name, but this should work out of the box on any online server.
Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

I tried one final test. I replaced the code (between the final else statement and the closing php tag) of language.php with the following:$msg = $msg . "Language: " . $language;$_SESSION['lang'] = $language;$msg = $msg . "Session: " . $_SESSION['lang'];$_COOKIE['lang'] = $language;And added this to my home.php:<p><?php echo ' Message: ' . $msg; ?></p>And that gives me:Message: Language: is Session: isSo I know for sure that both $language and $session work. But when I refresh it's gone. I'm gonna send 1&1 a long letter of complaint!

Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

I showed you the entire thing. I was just a test code without any UI. Just wanted to get the logic right before I worked on the UI. I'm using PHP5 according to the details I get from 1&1.

Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

Well I don't think there's anything futher you can do. I appreciate your time and effort. I contacted my host telling them I'd stop doing business with them if they didn't fix it. That should do the trick!

Link to comment
Share on other sites

I showed you the entire thing. I was just a test code without any UI. Just wanted to get the logic right before I worked on the UI. I'm using PHP5 according to the details I get from 1&1.
I'm with 1&1...more than half the time, I can't even sign in to the CP or check my webmail that doesn't have redirects set up...yet my dad can. It's bloody annoying. Not impressed with them, and not necessarily staying with them.
Link to comment
Share on other sites

Guest Stefán Örvarr Sigmundsson

Omg, I sent 1&1 a very detailed e-mail of my problems and they replied the next day saying something so unrelated to what I was saying. And then just "please contact us if you have any futher questions". I bet the person who replied didn't even bother read through my e-mail!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...