how to setcookie in right path? - php

classlogin.php
/*set cookies*/
$cookie_name = 'check[user]';
$cookie_value = 'check[authID]';
// start set cookie //
//** cookie need to change right away after user press login**//
setcookie($cookie_name,$value,time()+(60*60*24),'','','',TRUE);
setcookie($cookie_value,$valueID,time()+(60*60*24),'','','',TRUE);
index.php
<?php
print_r($_COOKIE);
?>
my folder root.
root
-->class-->classlogin.php(set cookie here>
-->directory-->index.php(check cookie here>
my intetion is to set cookie at classlogin.php , i checked $_COOKIE at classlogin.php , it exits.
but when i try to access cookie at index.php , it was empty, i search online it said my cookie need to have a domain name ? so i can access there. but i tried many domain noting work.
can anyone help me ? how i set my cookie in right path ? i am using wamp localhost.

Related

Cookie set in subfolder isn't set in root - PHP

I'm trying to set a cookie in a subfolder /admin/setcookies.php . I'm using this code for doing that:
setcookie(
"username",
$myusername,
time()+60*60*24*365,
"/",
$_SERVER['SERVER_NAME'],
1
);
Now when I test if my cookies are set from the root : /testcookies.php , I can see they aren't actually set. And when I do the same from /admin/testcookies.php , they are in fact set.
What am I doing wrong? The domain name is correct, and the path is set to the root... I don't know what else could be wrong at this point.
The 6th parameter of setcookie() is set to true:
Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
Your page at /testcookies.php must be accessed through HTTPS for the browser to send such a cookie.

setcookie not setting for the following code

set cookie is not setting the value for the following code.
<?php
session_start();
ob_start();
unset($_SESSION['adminname']);
session_destroy();
if(isset($_COOKIE['adminremember_me'])) {
$past = time() - 100;
setcookie('adminremember_me', gone, $past);
}
header("Location: login.php");
exit();
?>
Cookie is not deleting as setcookie donot works though an error message is not displayed.
Interesting part is that i have another file with same code structure but with different cookie name for normal user logout and that one works.
I moved the admin logout file which was in (htdocs/site/admin/)to (htdocs/site) and now logout works!!! seriously what change didit make?
You can have multiple cookies with the same name but different paths. So if you script is in /folder1/folder2/mypage.php, you can have 1 cookie with the path /folder1 and another with the path /folder1/folder2, and both cookies could have the same name.
My guess is the cookie you are trying to delete belongs to a different path (by default, if you don't specify a path, then it assumes the folder that the script is in). To delete it, you will have to manually set the path parameter to match that of the cookie. For example:
setcookie('adminremember_me', gone, $past, "/");
or
setcookie('adminremember_me', gone, $past, "/folder1/");
To see what the path is on the existing cookie, you need to use your browser's cookie viewer to see what path is set on it.
Edit: to answer the question in your edit, when you moved the location of your logout file, you moved it to be in the same folder as the path that was set on the cookie (so the default value was now the same). If you want to move the script back to the old location, just explicity set the path to whatever the folder was where it worked

PHP not retrieving cookies

This PHP code is supposed to write to a file in a folder specified by a cookie:
$user = $_COOKIE["username"];
if( $xml = file_get_contents("$user/docs.xml") ) {
But it just says the file /docs.xml (not specifying the folder) just the file and not the cookie value, apparently doesn't exist, because it's not getting the cookie but why?
Could it be that I'm trying to get it from a different domain?
Try setting the cookie like so
setcookie("Name", "Value", $time, "/");
The / at the end make sure the cookie works throughout the whole site, not just the folder where it was set.

php session value losing while moving to one page to another

I have made a simple website in which user can log in.my problem is when a user enter the address http://www.mysite.com/signin.php and after a successful log in he manually enters the address as http://mysite.com but from that page session variable is not getting. How do I make same session to both www.mysite.com,mysite.com Are there any settings in the php ini file or how do I manage to make same session to both addresses?
if(verify($password,$pw['password']))
{
$uid=$pw['user_id'];
$_SESSION['login_status']=true;
$_SESSION['user']=$uid;
}
i have a page checksession.php
<?php
session_start();
var_dump($_SESSION);
?>
when i login from http://www.mysite.com/signin.php and checking http://www.mysite.com/checksession.php its showing session values but from http://mysite.com/checksession.php it showing nothing.(differance is in address one with www,another without www)
I usually force www. or no www. in my .htaccess file using redirects.
Your problem is probably your PHP_SESS_ID cookie domain not beginning with a "."
session_set_cookie_params ( 3600 , '/', '.example.com');
That should set it.
Add this line before your session_start();:
ini_set('session.cookie_domain', '.mysite.com' );
This should tell PHP to include all subdomains of mysite.com in the same session, including 'www'. Note the period before the domain name.
A different Approach is to use session_start(); inside config.php file and include that file on the top of the code of every page where sessions are required otherwise U will not be able to get the value of SESSION variable..!!
use https:// before the page address call..

SESSION not working on server

i have domain and i created a sub domain as well with the name www.join.domainname.com, now the problem is i start session on the main domain login page that is www.domainname.com/support/login.php
all the pages in same domain working properly with session but when i am trying to check the session
on : www.join.domainname.com/member.php
i am not getting anything i don't know why?? Plz help me to solve the issue, here is the code of www.join.domainname.com/member.php :
session_start();
$session_key = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 'empty';
echo $session_key;
it return the result empty.
You have to set the session cookie domain to .domainname.com so that it can be accessible to all of its subdomain.
you can use the session_set_cookie_params to do this.
session_set_cookie_params(0, '/', '.domainname.com');
session_start();
Alternatively, you can set the session cookie domain with ini_set
ini_set('session.cookie_domain','.domainname.com');
From my previous experience to make your session usable across domain/sub domain you need to use the session.cookie_domain setting e.g
// Start the session
DEFINE('COOKIE_BASE_DOMAIN_NAME', '.domain.com');
$some_name = session_name("domain-name");
ini_set('session.cookie_domain', COOKIE_BASE_DOMAIN_NAME);
session_start();

Categories