iPhone web app, not storing cookie - php

I am working on a web app for the iOS. When the app is opened, it check's to see if the user has a cookie with the users email stored in it, then either lets the user proceed to the homepage, or redirects the user to the authentication page.
This works perfectly when using safari. The problem I am experiencing occurs only when the app is stored on the home screen. It seems like the home-screen web app deletes the cookie right when the user exits the application.
Any advice on forcing the app to store that cookie would greatly appreciated.
Thanks,
Peter

The reason its not sticking around is because the timeout parameter is not set.. if it is blank or 0, then the cookie will be deleted when the uiwebview is closed..
so you can do as the other poster suggested..
setcookie("TestCookie", $value, time()+3600, "/");
,but the reason that works is because of the timeout value being set

There is a parameter path for the setcookie function which you might want to use so that cookie is created just about from any page:
The path on the server in which the
cookie will be available on. If set to
'/', the cookie will be available
within the entire domain. If set to
'/foo/', the cookie will only be
available within the /foo/ directory
and all sub-directories such as
/foo/bar/ of domain. The default value
is the current directory that the
cookie is being set in.
So try adding '/' as the fourth argument to the setcookie function eg:
setcookie("TestCookie", $value, time()+3600, "/");

In case it helps anyone else; I was saving the cookie via an unload event, which worked fine on desktop, just not on the iPhone.
Nothing to do with cookies, just had to save-as-I-go...

You are not able to get the session on the iPhone because cookie is disabled.
Please go to Safari>Settings>Accept Cookies in your iPhone and set it to accept from Visited.
Then you will be able to create the session in PHP.

Related

Issue in setting cookie for subdomains in PHP

I tried to set cookies for embedded shopify app in php.
setcookie("user", 'test#domain.com', time()+3600, "/", "example.com", 1);
It works properly but if we are logged in with 2 shopify sites and if both opens the apps together in same browser, cookie value gets updated and same cookie gets set for both which causes same data to show in both places.
Cookie/Session works based on domain and will be unique.
suppose your app domain is example.com and you save logged in domain in session as
$_SESSION['logged_in_store'] = 'store1.myshopify.com'
when the second store login to your app this variable will simply updated as single session will work on this domain.
As a solution you can make use of wildcard subdomain
enable wildcard subdomain from your DNS.and when store login to your store redirect them to unique subdomain..... like store 1 will run on below subdomain
store1.your-app-domain.com
store 2 will run on
store2.your-app-domain.com
so on.. each store will run on different wildcard subdomain. and this will resolve your session / cookie conflicts.
Hope this will help.

Cookie Saves as two different urls

On my website, when users sign up or login, my site sometimes saves the cookie to "mywebsiteurl.com" and sometimes saves it to "www.mywebsiteurl.com". In doing this, my code only works half the time. Is there a way I can fix this issue from happening?
Set cookie in your PHP for your root domain and setcookie() will automatically make it available for all subdomains:
setcookie('cookiename', 'cookievalue', $someTimeToExpire, '/', 'mywebsiteurl.com');

Set cookies for one page only, not send back to server if user browse other page

Could cookies only set/get for one page only?
If I browse other page on the same root domain with that page, the cookies are not sent to server. Or are just not sent at all but still can access through jQuery cookie plugin.
I mean the cookies are only on client side, it will be very good to not send them to server.
My case is: On every page I need to store some cookies in browser and they differ depending on URL of page.
So if user browsed about 30 pages, browser will have 30 cookies and all of them will be sent to server when browsing any page.
That is not good anyway.
Thanks for any help.
setcookie($name, $value, $expire, $path, $domain);
You might want to set the $path argument. See the setcookie reference.

PHP Can't read cookies?

I like to use PHP to see if a cookie PHPSID27258STATUS is present with the value COMPLETE en if so do stuff.
In google chrome (in Options) I can see this cookie is present and has the value COMPLETE.
If I run this PHP script I get 'Not Set'. What am I doing wrong here?
$cookiename="PHPSID27258STATUS";
if (isset($_COOKIE[$cookiename]) && $_COOKIE[$cookiename] == "COMPLETE")
--update
The cookie is set by "limesurvey" an open source survey platform. Although its probably not the best way. I use limesurvey to have a small survey (iframe) on an site i'm building. I like to let the survey disappear on the next visit when it has bin posted.
Limesurvey runs from the same host (localhost now). And is in a sub directory of the site.
I guess (but there's a lot of code in LS) this is how the cookie is made after an poll/post is completed.
$cookiename="PHPSID".returnglobal('sid')."STATUS";
setcookie("$cookiename", "COMPLETE", time() + 31536000); //Cookie will expire in 365 days
You can only read cookies which belong to the same domain as the reading script. For instance if the cookie PHPSID27258STATUS was set by domain xyz.com, you can not read it using a script on abc.com. So make sure the domain of your desired cookie is the same. Also show us the code part where you are setting your cookie.
Edit:
setcookie($cookiename, "COMPLETE", (time() + 31536000) , '/');
Try setting the cookie with this code:
setcookie("cookiename", "cookievalue", time() + 31536000, "/");
This makes the cookie available to the whole domain, I recently encountered the same issue and when I tried this, it made it work, the reason is, your browser stores cookies for both domain.com and www.domain.com so you never know which your setting and getting from, it's good practice to set the domain even if you don't have this problem.
You cannot access a cookie immediately after you set it. At least last time it was like that. Make sure you do not have this issue. If it is not may be you have a problem setting the cookie, and in that case please post that part as well.
You mention that the cookie is set by a program running in its own sub-directory. You don't mention whether the cookie itself is set to be in that sub-directory, but I suspect this is where your problem is.
If a cookie is set to a path, then it will only be accessible to pages within that path. This behaviour is described in the PHP setcookie() manual page.
When you're setting cookies from a page within a sub-directory, then in order for the cookie to be accessible to the whole site, setcookie() needs to be called with the optional path parameter set to "/".
You state that the cookie is being set by LimeSurvey. I don't know this software, but you should be able to look at the source and see whether it's using the path parameter when it sets the cookie. If not, your best option would be to modify it so that it does. Then the cookie will be accessible to the whole site. (It would be quite understandable if the LineSurvey developers had chosen not to set it for the whole site, because it would allow the software to be run as a more isolated entity from anything else on the site).

Strange unset cookie problem

I have a strange problem to clear Cookie via PHP.
Lets say if I have a domain neobie.net
I store "remember user login" cookie name as "USER_INFO" which contains string to identify user login in the next time of revisit.
now using firefox, I saw that I have 2 cookies USER_INFO with domain "www.neobie.net" and ".neobie.net" with expiration date of 1 week later.
I wrote a logout.php script, which clear the cookie of different domain (.neobie.net, www.neobie.net, neobie.net) to ensure that USER_INFO cookie is completely cleared for different domain.
Now is the problem.
The user isn't able to clear the cookie when user visit logout.php
I found out that, I have to manually delete the cookie with domain "www.neobie.net", leaving the ".neobie.net " intact, then only the cookie can be cleared.
So, I have to make the php script to setcookie USER_INFO on ".neobie.net", and prevent it to set cookie on "www.neobie.net" to make the logout.php script work.
But I don't understand why I couldn't clear the cookie for "www.neobie.net" (with leading www. , tested on firefox and chrome)
You have overlapping cookie domains. www.neobie.net will receive cookies set on the .neobie.net. So there is no need to set the same cookie on both domains.
If your logout URL starts with http://www.neobie.net, you should be able to clear cookies on www.neobie.net domain. A HTTP header trace will help.

Categories