Yes, I know this has been asked a thousand times.. But, I've still been unable to find any specific fix that seems to work every time. I've tried many of the fixes people have suggested and I'm still having the same issue as before.
So, I run a server with a setup of multiple domains. They're all on the exact same server, and there is no transfer between servers here.
carnal.ueteribus.com <--- The Cookie is read and displayed here.
www.ueteribus.com <--- The login script is hosted here.
Basically those are the only two domains, and I'm trying to get the information from WWW to transfer to Carnal. Which is easier said than done.
Currently I've been trying to use
ini_set('session.cookie_domain', '.ueteribus.com');
Which hasn't worked, or maybe I have it programmed wrong. Anyways, any help would be very appreciated and if any additional information is required I am more than happy to provide.
NOTE: I do not have access to the PHP.ini, the company has denied such access.
session_name('LoginSession');
session_set_cookie_params(0, '/', 'ueteribus.com');
session_start();
I've also tried that
That seems to work in creating a named Cookie, but I couldn't figure out how to call it. My script wouldn't work anymore to call the actual login status, and I couldn't figure out how to fix it to ensure that this was even working.
<?php
session_set_cookie_params(0, '/', '.ueteribus.com');
session_start();
if (isset($_SESSION['error'])) {
echo $_SESSION['error'];
}
?>
Maybe something like this:
session_name('shared-name-between-sub-domains');
session_set_cookie_params(0, '/', '.domain.com');
session_start();
The absolute first two things in your scripts need to be:
ini_set('session.cookie_domain', '.xxxx.com');
session_start();
in that order, and any session's begun before adding that code will become inaccessible.
This assumes that both domain1.xxxx.com and www.xxxx.com live on the same server and use the same instance of PHP. It is impossible to share PHP session data across servers without writing your own custom session handler.
If there are no other domains on the server you may want to simply set session.cookie_domain in your php.ini.
Related
I've following two path
path1- test/hello/hello.php
path2- test/hello1/hello1.php //notice the one in the directory after test/
hello.php
<?php
session_start();
$_SESSION['name1'] = 'abcd1';
?>
other file is
hello1.php
<?php
session_start();
echo $_SESSION['name1'];
?>
In one computer I am able to get the value in hello1.php
In another computer I am not getting value in hello.php
In both the PC I had clear storage, ran Hello.php for session to set. Hello1 has value in One pc , in another I don't have value.
What might be the issue?
Also, what is the correct work, In general will I get session value Outside test folder OR everywhere inside test folder or only in the parent directory of the file where session was created.
Please don't forget the original issue.
Also one comment I don't know if its realated, I have 2 xampp in 2 drive in the pc where hello1.php gave the value. does'nt it affect anything?
In short I want concept of Session WRT to directories/ also about framework, does framework make restrictions to accessing variable outside their core project folder.
You told us nothing about how your PHP is configured, so there is a very extensive list of things which could be going wrong - far too many to list here. Make sure your error reporting/logging is working correctly (and that no errors or warnings are being produced). Have a look at the path, name and value of the cookies being emitted by the server for both pages using firebug or developer tools.
Sessions are preserved across requests and use cookies set on the browser to access the data. Your computers have different cookies, and thusly different sessions.
Read more about this in another answer
Ultimately you need to think about if you are using the right tools to accomplish this goal.
As first, I know a lot questions are like mine, but I really don't know what I'm doing wrong...
As you might've guessed, I've a PHP script involving sessions.
Everything works like a charm, except setting the lifetime of my session.
I want to keep the session active for two weeks, but instead my (Chrome) browser says it's set to xpire after the browsing session (and it does). My PHP script:
session_name('DSWLogin');
// Naming the session
session_set_cookie_params(2*7*24*60*60);
// Making the cookie live for 2 weeks
session_start();
// Starting the session
It really doesn't work.
Thanks in advance,
Isaiah
Rewrite your code as
session_start();
setcookie(session_name('DSWLogin'),session_id(),time()+2*7*24*60*60);
I have been trying to maintain session vars between two subdomains and found it impossible. I ended up creating 2 minimal PHP web pages as a test bed, one I call 'test 1' just sets
$_SESSION['test'] = "Fred";
and has a hyperlink to 'test 2' which simply tries to echo the value of $_SESSION['test'] to prove it's worked, or not. I place 'test 1' in my www domain and 'test 2' in my sub domain. I try various version of what should go in the header, from various sources. Here are the main 3 (and of course their variants):
ini_set('session.cookie_domain',substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
session_start();
or
ini_set('session.cookie_domain','mydomain.com');
session_start();
or
ini_set('session.cookie_domain', PHP_INI_ALL);
session_start();
or
session_set_cookie_params(0, "/", ".mydomain.com", false);
session_start();
I find that I get an identical result in every case. The session is not carried across the subdomains and page test 2 has no idea what value I set $_SESSION['test'] to. Yet there seems to be plenty of certainty around the 'net that one of the above methods should work. Any idea what could be going on, especially since I am using minimal pages to test the mechanism (no side effects that I can see)? By the way I am on a shared server, if that's pertinant here.
Thank you for your thoughts. Frank.
Edit.
I fixed it. The problem was caused by Suhosin. See detailed answer at the foot of this page.
Ok I nailed it and it was a stinker.
Suhosin's suhosin.session.cryptdocroot option was the entire cause of the problem. When the session encryption key is based on the DocRoot it causes the subdomains to fail to see each other's session variables when the base domain and the subdomains are served from different directories. This leads to the session vars on the server being stored in different folders and hence they are not visible to each of the corresponding domains.
Solution. Simply add these 2 lines in your php.ini file:
suhosin.session.cryptdocroot=Off
suhosin.cookie.cryptdocroot=Off
A 48 hour nightmare to track down, 4.8 seconds to fix.
I have it working, setting a session name and session cookie parameters:
$some_name = session_name("some_name");
session_set_cookie_params(0, '/', '.some_domain.com');
session_start();
I'm building an autologin system using cookies, but one fundamental part of the functionality of the cookies fails: they are non-persistent over different sessions - or even pages!
In my login script, I set the cookies like this:
setcookie('userID', $userID, time()+86400); // (edited after replies)
$userID has a value.
Then I print the $_COOKIE variable and it says array(['base_usid'] => 1); So that's good, but when I click the home page and print the $_COOKIE variable there, it says NULL.
Does anyone see the problem?
Cookies should have a time value for how long they should stay... Check http://php.net/manual/en/function.setcookie.php
In other words, change it to: setcookie('userID', $userID, time()+86400);
to make it stay for a day for example.
Aah, I've learned something new about cookies :) They have a path and they are only available on that path (the directory they were created in). I created the cookies on /user/login, and then tried to read them on /news/index. Won't work.
In the past I used to build websites with all files in just one folder (I know it's bad), so I didn't know of this cookie property. Sorry, I should have read the manual better...
Thanks for your help!
P.s.: Typing print_r($_COOOKIE); won't speed up debugging. :(
Cookies need an expiration time. Otherwise they are by default destroyed when a user closes his browser.
Try this instead
setcookie("userID", $userID, time()+3600);
This will last for an hour. Make the number bigger to have it last longer.
To unset / remove it, change the plus + to a minus -
:)
If its still not working after you've set an expiry time (and you've checked the clocks on server and client are correct) then have you checked that the cookie is being sent? Sounds like the problem with 'headers already sent'. Which would also imply you have a problem with error reporting / logging.
C.
Do you want to learn how to build CMS systems and login managers, or do you want to build an app... ?
Hate to do this, but my answer is : don't build your own login system. Instead, go grab some framework like CodeIgniter, Kohana, or even drupal or Joomla. If you are building a login system as a learning experience to understand how cookies work/etc, then fine.. go ahead.. as long as you don't plan on putting it into some production site. Otherwise, grab a well tested framework and use it.
I have an application with multiple regions and various incoming links. The premise, well it worked before, is that in the app_controller, I break out these incoming links and set them in the session.
So I have a huge beforeFilter() in my app_controller which catches these and sets two variables in the session. Viewing.region and Search.engine, no problem.
The problem arises that the session does not seem to be persistant across page requests. So for example, going to /reviews/write (userReviews/add) should have a session available which was set when the user arrived at the site. Although it seems to have vanished!
It would appear that unless $this->params is caught explicitly in the app_controller and a session variable written, it does not exist on other pages.
So far I have tried, swapping between storing session in 'cake' and 'php' both seem to exhibit the same behaviour. I use 'php' as a default. My Session.timeout is '120', Session.checkAgent is False and Security.level is 'low'. All of which should give enough leniency to the framework to allow sessions the most room to live!
I'm a bit stumped as to why the session seems to be either recreated or blanked when a new page is being requested. I have commented out the requestAction() calls to make sure that isn't confusing the session request object also, which doesn't seem to make a difference.
Any help would be great, as I don't have to have to recode the site to pass all the various variables via parameters in the url, as that would suck, and it's worked before, thus switching on $this->Session->read('Viewing.region') in all my code!
Try setting the security setting in your /app/config/core.php file to medium or low. That solved a session problem I had.
i had the solution or at least that work for me
you try to pass from controller reviews action write to controller userReviews action add right???
check that your controller userReviews must end whit php tag "?>" and NO MORE SPACE
SO if you have someting like this
line
999 //more code lines
1000 ?>
1001
your session fail
you have to had this
line
999 //more code lines
1000 ?>
sorry for my bad english
soo you
It would appear that unless
$this->params is caught explicitly in
the app_controller and a session
variable written, it does not exist on
other pages.
That sounds like the proper behavior unless you are posting data from page to page. If you want any variable to persist, it should either be set in the model (where it will persist with the association), or passed on in a function, or set in the session explicitly using the session component:
$this->Session->write('Viewing.region');
(see: http://book.cakephp.org/view/398/Methods)
On a related note, I've had most success with sessions stored in the database. Run the file from app/config and set it to db. See if that helps.
Also, do the Cake core tests for the session work?
Might it be this problem? Essentially, cake's session resets if the user-agent changes
It's a shame that I ran into this very problem you mention a few days ago and now I cannot find the link that helped me solve it.
Also: are you using database or plain php sessions?
I'm going to go out on a limb here without being able to look at your code, but might it be possible that your "reviews" controller (or whatever) has its own beforeFilter() and doesn't call its parent's beforeFilter() explicitly?
This has burned me before...
I got some issues like this. Session set using some controller was not available in another , controller . I could clear the issue after spending few hours . There was a white space afer the end of php tag at the bottom . After clearing the line and white space after the last ?>
tag worked fine .
I had this problem when moving a CakePHP site. My problem was that the session directory wasn't writeable. You should make sure the folder app/tmp and all it's subfolders (including sessions) have permission 777.