SESSION lost after location.reload() - php

I am trying to make login using ajax to send username and password to my php script for checking if there is any record in database, on my ajax success i want to reload the page in order to show something if session is created. I use session start on very top of my pages. Also on local server my login works perfect, but on ipage server i have this problem with losing session...(i tested it using var_dump($_SESSION) and its always array (size=0) empty)
I have also tried using this on my very top of pages:
session_save_path("your home directory path"/cgi-bin/tmp);
session_start();
but this also didn't work...
Also i have tried to change session.save_path but still same.
I am sorry for my bad English.
EDIT:
I`ve tried using form to create login but same issue still going on. Its clear now that something with the server is wrong.(maybe with the php.ini)

It turns out that my host changed servers and started using a different session save path other than /var/php_sessions which didn't exist anymore. A solution would have been to declare ini_set(' session.save_path','SOME WRITABLE PATH'); in all my script files but that would have been a pain. I talked with the host and they explicitly set the session path to a real path that did exist. Hope this helps anyone having session path troubles.
Location of session folder:
/domains/account/session
location of index.php
/domains/account/public_html/index.php
What I placed in index.php at line 0:
<?php
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session'));
session_start();
This is the only solution that worked for me. Hope this helps someone.

Maybe I'm late with my answer but I think it will be helpful to people who run into the same problem as I did.
I'm validating Login form by using jQuery and AJAX. If there are no matching records within the database then PHP script returns error but if everything is all right then PHP script stores email,passoword and loggedIn variables as sessions and returns success to jQuery. Then I put location.reload();
which apparently deletes session variables (all except boolean). So I used location.reload(true);. This solved my problem.
You can also read something more about this on W3Schools .

Related

Session lost in sub domain url in apache server

I have a session lost problem in sub domain url (test.example.com) after login. But the same code working fine in (i.e example.com). I tried session.cookie_domain but same error exist. Please anybody help in this regards
I had the same experience like you hundreds of times which I finally found the problem. You don't need to worry if cookies enable or disable. You just have to re-declare parts of your scripts (variables-the data which you want to display) in a if else condition. Just redeclare the same thing using !isset and then else with the same variables mentioned twice in the if else condition, if you understand what i mean.e.g if (!isset etc){A+B} else {A+B}. It will call back the same variables over and over. And make sure start session is above the page and define a session e.g session['id'] on that page which you claim missing.. no need to worry about headers or white lines, it has actually nothing to do with it. I've tried it a million times. You don't need to bother using ajax etc. PHP can do almost everything. cheerio.

Laravel sessions regenerating on every load

I'm having a lot of problems and I really can't seem to find a solution.
Yesterday, I finished up doing some work on a vagrant box in Laravel. When I shut down the computer, login functionality was working fine.
On opening everything up today I find sessions are no longer working.
I've tried everything I can think of but I cannot log in now and flash data doesn't work.
Every page load a new session is created - e.g using the native driver, if I login, it creates two sessions - one for the login page, and one for the posted login page.
If I use a database driver I get the same result. Every time I click login, I get a further two rows of session data
I can't tell what's happening with the cookie driver but I've tried it and it's not working.
I've tried in Chrome and IE and neither will login. I've tried deleting the contents of the storage folder multiple times, and emptying cookies on the browser side. I did think it may be to do with the time being different on the vm and the local machine but they're synchronised.
Any ideas what could be causing this? Anyone come across this issue before?
edit: I've also now recreated the virtual machine and the problem still exists
second edit: I've now started from scratch using a digitalocean VPS with the same results.
I've stripped out everything in my routes file and all it now has is the following
<?php
Route::get('/sess/set/{value}', function($value) {
Session::set('testv', $value);
Return 'Set ' . $value . ' - ' . Session::get('testv');
});
Route::get('/sess/get', function() {
Return 'Get ' . Session::get('testv');
});
I visit the first page and it shows whatever value I put into the session. Hit the second page and you only get the 'Get ' part without any session value.
session_attributes always has a _token field, I've tried changing the name of the session and the domain and still can't get sessions to work.
edit3: for anyone who comes across this, I never identified the issue. I started a completely new project, copied my controllers and views across, recreated my routes file and everything is now working - although I'm half expecting to be updating this post tomorrow to say it's broken again!
My problem was that I had echo statements in my function. It was keeping the session from being properly created/stored.
I had an echo before my 'return Redirect' so the redirect didn't get the session...
public function login()
{
// auth
if (Auth::attempt(Input::only('email', 'password'), true))
{
return Redirect::route('companies.index');
}
// failed -> back to login
return Redirect::back()->withErrors(['email' => 'Login failed.'])->withInput();
}
Details: http://brainwashinc.com/2014/02/17/laravel-sessions-not-working-in-4-1/
I had this exact problem and with almost identical environments as well. As you experienced as well what ended up working for me was starting a totally fresh Laravel 4.1 project and then copying everything over. However I did also find that by changing one config variable I was able to fix the problem:
In /app/config/session.php change the lifetime config item to something higher than 0. This fixed the issue for me.
I had this same issue and already changed my session values to the 4,1 version. I logged into our server and ran the composer update command, after it finished everything worked fine.
composer update
I spent a whole day with the same issue (session regenerated with almost every request), and could not find anything helpful online. Posting this here in case somebody has the same issue.
Basically the problem was that the session cookie was not set correctly due to template errors. So if you run into this, first check that the session cookie is set with every request via http headers.
In my case I wrongly used something like this in some of my templates:
#section('test')
<p>Some content here</p>
#endsection
This is wrong!
In Laravel 4 it needs to be #stop instead of #endsection:
#section('test')
<p>Some content here</p>
#stop
Using #endsection made the app not finish correctly, so no session cookie was set. There is no trace of anything being wrong in the logfiles however. BTW, this kind of error also leads to after-filters not being applied.
I also has this problem, and it turned out to be with outputting data early - I was already echoing some debug text in the controller action, and then tried setting a session variable for the first time - the session just wouldn't persist, and kept recreating each time - once I removed the output, it worked fine (and fine from then onwards)
I upgraded from Laravel 5.1 to 5.2 and had the issue of numerous sessions being created on every page load. It didn't matter which session driver I used, nor did changing anything in config make it work. (I tried every suggestion on every StackOverflow result produced by Google.)
The solution for me: remove "web" middleware from your routes. Apparently, web middleware is included automatically in 5.2+, so if your routes ALSO specify it, you wind up with multiple sessions being generated.
There are many reasons this can happen, I just ran into this problem myself. The fix for me was to change:
public function getAuthIdentifier()
{
return $this->username;
}
to
public function getAuthIdentifier()
{
return $this->getKey();
}
on the User model.
I've experienced issues related to this. The session file wasn't created every time but sometimes I just can't get the session variable displayed. After hours of experiments I found that the problem was related to Debugbar.
If you're using Debugbar and having session issues, disable it and try again to confirm. There's an issue reported here.

PHP file redirecting user when it is not supposed to

I'm making a login script for a Server CP although it doesn't seem to work as intended, I can login but if I go to the index page it doesn't redirect me to the admin page, code here:
All Code: http://pastebin.com/DC8fVtD5
Could anyone shed any light on this for me please? :) The problem is with the cookies or the sessions but I'm not sure how to fix it. The script needs to check user input against a mysql database and see if they have an account for the website and if so, log them in. It seems to be logging me in although admin.php seems to be redirecting me to index.php when $logged != 0. I haven't been able to find a fix for it and it is driving me nuts. D:
Don't forget, that the variable
$logged
if set in one script isn't any longer set in the next script execution: If you redirect using header( 'Location: somewhere' );, the current script terminates and a new script gets started by a new HTTP-request forces by the Location-header.
Please use PHP's session-feature to keep state through multiple requests/script-executions.

AJAX PHP and Sessions Strange behaviour

I've read quite a few solutions on the web, but none really works for me. So Coming back to Stackoverflow:
The Issue is as following: My session gets created, it get's passed trough the pages, and it is usually still running if uploading a picture or changing information via AJAX. But sometimes, and that's not something I can reproduce - It just changes the Session ID as soon a picture is uploaded - I just get a new Session ID on the upload script - meaning if i browse trough the page it's the old ID but the picture of course won't get saved as it's a new session_id - and what's even more funny this one will stay the same for picture uploads until it's destroyed! So in fact I'll have 2 session_id's for the same session.
I am getting unpatient so it would be really nice if somebody had a clue for me. Of course session_start is executed at the top of every page ;).
I am doing quite a few unneccesary mistakes since a couple of weeks.. Oh well of course there was nothing wrong with the code as it was... The mistake was on my side as of I forget that flash doesn't pass anything if not told to.
And as I use the swfuploader libary, oh well I think you can imagine the rest.
Don't ever forget to pass your session id if using flash!

PHP cake subdomains session link problem?

i have problem with php cake session and subdomains. I have setted all successfully right. When i try to go from
subdomain1.domain.com
to
subdomain2.domain.com
all works nicely when i put these browser url... problem comes when i try to make this with <a href=""> tag, on redirected subdomain i will recieve new session id.
I cant explain to my self how can be this possible, pure php script works fine but in php cake is this bug! thanks for any suggestion...
ok, i debug whole php cake session component and lib, i figure out 2 sollutions
easier - set in core.php security level to low
Configure::write('Security.level', 'low');
advanced - make new config file for sessions, like Ivo said, its in tutorial http://book.cakephp.org/view/1310/Sessions ,most important thing is set
ini_restore('session.referer_check');
because by default php cake check referrer, and if it goes not form same domain it will cause generating new SESSIONID
I don't think it's related with cake.
By default, PHP will give you a session for the domain.
subdomain1.domain.com is a domain,
subdomain2.domain.com is another domain
domain.com is another different domain
www.domain.com is another different domain
All thoses examples are 4 distincts domains, with their own session.
If you want to share the session between many (sub)domain, you can try to set the session.cookie_domain variable, like this;
ini_set("session.cookie_domain","domain.com") ;
Try using this instead:
http://book.cakephp.org/view/1310/Sessions
Follow the directions to create custom configuration for cake's session saving.

Categories