Im having a strange problem.
I have a subdomain where a customer comes in with a specific URL
When the customer is recognized I set a cookie and redirect them to the main domain.
there I check for this cookie to hide some elements.
This is working great in Chrome, Firefox and even Edge on Microsoft, but not in IE11 and chrome on Apple machines.
When I type document.cookie in the IE console, I can see the cookie.
I display a cookie found message in the console which I can see in chrome etc. but not in IE.
So it looks like IE can't find the cookie, while it is actually there.
Is there someone who can explain this behaviour?
I finally found the problem.
When checking for the existence of the cookie, at first I used this line of jQuery code.
if (document.cookie.split(';').filter((item) => item.includes('cookiename=')).length) {
console.log('cookie found');
}
I briefly saw an error message in IE that pointed to this line of code.
so, I changed it to the, btw much easier, line
if (document.cookie.indexOf("cookiename=") != -1) {
console.log('cookie found');
}
which IE has no problem with.
Related
I have a very strange problem. I am using this simple code:
<?php
session_start();
if(isset($_SESSION['test'])) {
echo $_SESSION['test']."<br>session:".session_id();
}
else {
echo "<h1>Session inexistante. Crée</h1>";
$_SESSION['test']=2;
}
?>
1- I delete all cookies, session, cache...
2 - I go to my page and get the message "Session inexistante. Crée" (meaning "Session does not exist. Created"). This is normal
3 - I update this page, and I still get the message "Session inexistante. Crée". This is not normal !
4 - I update again and the session works correctly
Here are screenshots with the detail of HTTP requests
- We see that on the first loading the session does not exist and is requested by Chrome
- On the second loading Chrome makes a correct request giving the PHPSESSID, the response is correct because it does not contains an other PHPSESSID so it seems PHP is happy with this PHPSESSID. But the message displayed is still "Session inexistante. Crée" !
- We see on the third loading that the session ID given by PHP during the first load of the page was correct. So why it has not been recognised on the second loading?
I am using Php5.6 on a shared server.
This issue is rather random. I have no difficulty to reproduce it even if somethimes everything works well. Sometimes the issue is on the third loading.
This issue is more general on my website. Many users with many browsers have a similar issues.
There is a similar problem here: but as you see I have no 404 error or 302 redirect.
Thank you for your help !
Basic situation and basic relevant info:
I have a php code that executes before the opening <doctype> tag. The hope was to (if necessary) send a redirect based on user's browser's language preferences before anything else loads.
The script attempts to do two things based on highest supported language preference:
Use php: setcookie() to create a cookie with the two-letter language code.
Example cookie name = value: x_language = es
Use php: header("Location: " . $requestedSite); to redirect to a subdomain,
Example domain: es.domain.com
Example:
if (isset($_COOKIE['x_language'])) {
-Determine correct subdomain based on cookie value-
-If not currently on that subdomain, redirect to it-
} else {
setcookie('x_language','es',time() + 31536000 ,'/','.domain.com' );
header("Location: " . $requestedSite);
}
The problem:
Firefox works perfectly. Chrome (and other browsers) fail to recognize the cookies at all.
I've boiled it down to this:
print_r($_COOKIE) works properly in Firefox, and returns a lovely, populated array.
print_r($_COOKIE) fails in Chrome, and returns an empty array.
This is the core of the problem, my function doesn't recognize the existence of a cookie because Chrome doesn't.
I've made sure every browser accepts cookies.
I've checked dev tools to make sure the cookie is in place on all browsers, (it is).
I realize a cookie's value isn't available until the next page load, but that isn't an issue here. Even after it is set, it won't read.
There is no output above the initial setcookie();
So how do I get Chrome (and other browsers) to recognize its own cookies?! Does anyone know why this would all work flawlessly on Firefox but fail elsewhere?
On a lark I decided to try this. I created a file that only contains:
<?php
print_r($_COOKIE);
?>
Again, I see the cookie array in Firefox. Meanwhile, in Chrome, IE, Opera, Safari, I get an empty array. Could this be a server issue?
OP returns with answer:
Alright, I'm adding this as an 'Answer' in case anyone else comes across this (totally bizarre) behavior and lands here:
It turns out my hosting provider was doing some seriously aggressive caching with my WordPress site that I was unaware of.
At the time I posted my question, I didn't think being on WordPress was relevant, but apparently it was.
Basically it was doing this:
With a clean Cache:
Visitor 1 visits the site.
The php processes and produces output as expected.
Visitor 1 is served php output (based on his browser's parameters and such).
Visitor 2 visits the site. Visitor 2 sees *Visitor 1's version of the site.
The php is processed once and only once per Cache-clear.
This caching behavior meant that accessing cookies through php was simply not going to work right, but accessing them with Javascript WOULD work.
(Important note: It turns out the above-stated caching behavior is disabled for any user viewing the site while logged into wordpress, and this is common behavior for WordPress Cache plugins. That is why I was seeing different behavior in Firefox than I saw in other browsers, because I was actively logged in with Firefox. This could be a helpful piece of information for someone out there.)
My solution:
Use Javascript to run an AJAX query to a .php file which would process the language preferences of the visitor and return the output as a 2-character code, (i.e. 'en' 'es' 'pt' 'de', etc).
Using AJAX to call php allowed me to use php's server-side access to a browser's language preferences while circumventing the super-agro caching of my host.
I hope this helps someone! And thanks to everyone who tried to help me out with this.
I was not having this problem with the code below. I was able to go to example.com and be redirected immediately to en.example.com and see the cookie in $_COOKIES. If I used en.example.com?set=fr I would be redirected to fr.example.com every time I tried example.com. Hopes this is what you were looking for!
<?php
print_r($_COOKIE);
if(isset($_GET['nuke'])) {
setcookie('x_language','',time()-1000,'/','.example.com');
echo 'It has been nuked!';
exit;
} else if(isset($_GET['set'])) {
setcookie('x_language',$_GET['set'],time() + 31536000, '/','.example.com');
$_COOKIE['x_language'] = $_GET['set'];
}
if (isset($_COOKIE['x_language'])) {
$redirect = $_COOKIE['x_language'].'.example.com';
if($_SERVER['HTTP_HOST'] != $redirect)
header('Location: http://'.$redirect);
} else {
setcookie('x_language','en',time() + 31536000,'/','.example.com');
$redirect = 'http://en.example.com';
header('Location: '.$redirect);
}
echo '<br />Cookie: '.$_COOKIE['x_language'].' Domain: '.$_SERVER["HTTP_HOST"];
?>
I have a register form, when user sign up, it will redirect him to his page.
All work fine in firefox and chrome, but in internet explorer. It looks like after the user information is saved, the session went off and it won't redirect the user to his page.
How can I fix this issue on IE?
$user = $this->_helper->model('Users')->createRow($signupForm->getValues());
if ($user->save())
{
Zend_Session::rememberMe(186400 * 14);
Zend_Auth::getInstance()->getStorage()->write($user);
$user->sendSignUpEmail();
$this->getHelper('redirector')->gotoRoute(array(), 'invite');
return;
}
I'v got a similary problem to create session in iframe on IE before a redirection, and this works for me :
Try to put in the Zend Action :
$response = $this->getResponse();
$response->setHeader('P3P', 'CP="CAO PSA OUR"', true);
See What does header('P3P: CP="CAO PSA OUR"'); do?
You seems to keep posting variations of the same question. There's nothing in your code which should work differently on different browsers. You need to debug this to see how far IE is getting, that will help you identify the root cause of the problem.
So, do some debugging to try and answer these questions:
Does $user->save() return true? (i.e. does IE go into the if statement)
If it does go into the if, what is in $user? Try var_dump($user);exit; inside the if to see what you get
Does $user->sendSignUpEmail(); get called?
If it gets to the redirect part, what headers are being sent? (You should be able to check this using IE's developer tools)
If you are testing in IE on a different machine to the one with browsers that are working, also check the system clock, as incorrect date/time can cause sessions to be expired immediately.
Im not sure where im going wrong. I have a form that when the contents of the form get processed and sent to the database it also sets a cookie
setcookie("bgremkey",$checkkey, time()+2592000);
then it will redirect the user back to the page they came from. All of this works fine (bar the cookie bit)
then i have it set at the top of every page providing there isnt an session active to check to see if the cookie exists and if it does to redirect but it wont work. im sure that the cookie is there but it wont pick it up
<?php
if(isset($_COOKIE['bgremkey']))
{
header("location:'Check.php?cklog=1");
}
?>
<script type="text/javascript" language="JavaScript">
var acookie = ReadCookie("bgremkey");
if(acookie.length != 0)
{
window.location = " Check.php?cklog=1";
}
this code doesnt generate any errors but it also doesnt do anything. i have tried putting it in the of the page but that didnt work so i then tried the body and that didnt work either
the check page does a bunch of other stuff but thats not the problem since the redirect never happens
i checked the cookies through chrome and the cookie exists and its path is / so the problem is definitely with reading them. it exists but for some reason cant be detected
http://php.net/manual/en/function.setcookie.php
setcookie("cookiename","cookievalue", $time); will only set it for the current path
Whereas: setcookie("cookiename","cookievalue", $time,"/"); will set the cookie for all pages/folders on that domain (note the 4th argument containing the path ).
If you press CTRL+SHIFT+J in google chrome, and click on the Resources tab, you can find the cookies and the path it is valid in. I'd check that out. perhaps this is why?
so i need a cookie set for 21 days on a browser when a user hits the site and everytime the user returns in that 21 day period i need to retrieve that value
if($_REQUEST['ref'] == "something"){
setcookie('something_value', "something" ,time()+60*60*24*21,'/','mydomain.com');
}
in the view
<?php if(isset($_COOKIE['something'])) { ?>
but when i view the cookies in safari and firefox i dont see "something"
am I missing something
Looks like you've swapped the first two parameters of setcookie. The first parameter should be the name of the cookie.
// prefix the mydomain.com with a . (makes it work on more browsers)
setcookie('something_value', "something" ,time()+60*60*24*21,'/','.mydomain.com');
I've also had that problem and putting a . in front of the domain name made wonders for me.
Do not view cookies in safari and firefox. Cookie is an HTTP header and nothing else. Do not rely on inner browser's mechanism. But rely on HTTP log only. Do you see your cookie in HTTP log?
what is it's name? "something_value"? Don't you mess something? ;)