I am facing a problem recently with PHP Sessions, on every browser refresh the sessions are getting lost.
I saw that the PHPSESSID is not being read in the cookies.
Then i tried this piece of code.
echo (isset($_COOKIE['foo']) && $_COOKIE['foo']=='bar') ? 'enabled' : 'disabled';
And it seems that PHP cookies are disabled.
My question is this: How can i enable the PHP cookies? What is causing this problem?
Is it a server configuration?
Thank you in advance. Hope to read an explanation from someone.
There are 2 places to do it, either in your php config, or in an htaccess file.
Config
find and change this setting in your php.ini file:
session.use_cookies=1
heres the docs on this and all the other cookie related switches.
note: if you dont know where youre php.ini file is, from the command line, type $ php --ini and itll show you where its located for the cli, the server version will likely be up one level in an apache2/nginx folder.
htaccess
check you dont have this line in your .htaccess file.
SetEnv session.use_cookies='0';
I have about 150 websites each on its own Apache virtual host running php7 on ubuntu 16. Each site has a config.php in the root dir with constants in it: define('MYVAR','myval');
I want to make a new constant that I will have to update every 2 months or so. I don't want to open each config and edit them. How can I have each site/virtual host be aware of this constant I want to set. I want to be available everywhere just like $_SERVER['REMOTE_ADDRESS']; is.
I do not want to create a file them symlink it to each site, thats messy. I would like to edit the apache config or edit a php config someplace to have a define() in it.
Is this possibe? or am I stuck sym linking a file or editing all my configs?
As pointed out in this link; PHP: possible to set constants in default php.ini file
You can't just invent variables in the PHP file, you'll need to auto-prepend a config file that has all the variables defined.
Ignore Below
You could store some global variables in your php.ini file.
The single php.ini file should be used by all your Apache virtual-hosts.
If you update your php.ini file and put a variable in like
my_custom_var = ABC123
Then you can access the variable with ini_get
echo ini_get('my_custom_var');
See more details on ini_get at http://php.net/manual/en/function.ini-get.php
I think you should try using OS environment variables.
Try setting the variables and then exporting them in your .profile file.
Ideally, this is where you should store sensitive credentials too. This way, even if your code is leaked, no one can get access to your existing server (never store Database or other credentials in your code)
You can either store your system variables in .profile or .bashrc file (I would recommend .profile). You can see how to set system variables here: https://unix.stackexchange.com/a/117470/209405
You can get the variables in PHP with the help of getenv($var) or with $_ENV[$var]. For more info, you can check here: http://php.net/manual/en/function.getenv.php and http://php.net/manual/en/reserved.variables.environment.php
The best non-intrusive way for me was to make sure mod_env was enabled then I added in the conf-enabled dir a conf file with the following in it
SetEnv MYLITTLEVAR thestringIwant
Then in php I can get it by:
print $_SERVER['MYLITTLEVAR'];
Tested and it works for me.
If want to make some variables globally available in PHP you need to add those variable in the apache config file.
if you're using ubuntu open /etc/apache2/apache2.conf file, And if you're using windows open httpd.conf. And add the following line.
SetEnv VARIABLE_NAME 'whatever value you want'
My phpmyadmin on a USB drive (I'm using EasyPHP Wamp ) is giving the following:
phpMyAdmin - Error
Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.
following Cannot start session without errors in phpMyAdmin
I have cleared my browser cache, but its still not working. Now I want to check where session.save_path points to. In php.ini I see
session.save_path = "${path}/tmp"
I have my wamp on a USB key named F:. does the above in this context mean F:/tmp ?
I have been trying to debug this problem for hours but couldn't. I have these two files:
My first file:
session_start();
$_SESSION['user'] = '1';
My second file:
session_start();
print_r($_SESSION);
echo $_SESSION['user'];
But The second file echoes an empty array. This works fine on my localhost but didn't work on online server. I also have 'register_globals' turned off in php.ini
If you are using cPanel goto cPanel > php.ini QuickConfig >
Check for
session.save_handler = files
session.save_path = /tmp
If you have still facing the problem ask your cPanel guys they will help you
i was also facing the same issue with ipage hosting now it is solved with Radhakrishna Chowdary`s help i did following-
login to your ipage hosting account
go to Additional Tools -> CGI and Scripted Language Support
there choose PHP Scripting
there you will see "Edit your php.ini file for PHP 5.3" click on
edit.
change value of session.save_path `s value to "/tmp"
its done working now :)
iPage requires you to specify the session_save_path() within your script. Like so :
session_save_path("your home directory path"/cgi-bin/tmp);
session_start();
Take a look at this iPage Knowledgebase article for further reference.
Try changing the session variables like $_SESSION['user'] to $_SESSION['userr']
as changing the variable names worked for me for php 5.3 on ipage
Changing 'session.save_path' is not working I tried this a lot but nothing fixed, I contact there support to fix the issue for me, but they said session is disabled in ipage, which is something very bad for cPanel site.
But anyway I recommend another hosting service since iPage doesn't support session which is something main in php development for login and save data.
I recently receive this from IPage customer service:
this mean that the "session.save_path" will never change in 'php.ini', you have to do it in your code as mentioned from there customer service.
Remember to add this line before session start
session_save_path('/home/your/home/directory/path/cgi-bin/tmp');
I have a problem where i am losing the PHP session between 2 pages.
The session_start() is included in a file called session-inc.php into every page requiring a session to be set. This works for all pages on the site except one particular page, member-profile.php. When this page is visited a new session with a different id (same session name) is set and used instead.
A few more details:
Session name is set manually
All pages are on the same server under the same domain name
If i put an additional session_start() above the include('session-inc.php') in the member-profile.php file, the session is carried over correctly
I have tried setting the session_cookie_domain and session.session_name in the .htaccess, this worked for this domain but it stopped the session being passed over to out payment domain
We are running apache 2.2.6 with php 5.2.5
Putting the session_start() above the include('session-inc.php') in the member-profile.php file is the quick and dirty fix for this problem, but i am wondering if anybody know why this would be happening.
Cheers
Will
According to PHP documentation, session_start must be called before any output is sent back to the browser-- could this page have a rogue CR/LF, Unicode byte-order mark or similar that is causing output before you include('session-inc.php')?
While migrating a legacy site from PHP4 to PHP5 I noticed a php.ini configuration setting that causes php to auto-start the session upon every request. It's an alternative to placing session_start() onto every page...
There are multiple ways to enable this setting:
Put the following line into php.ini:
session.auto_start = on
or put this into your apache virtual-site config or .htaccess file:
<IfModule mod_php5.c>
php_flag session.auto_start on
</IfModule>
and it should make $_SESSION changes available across all pages
I have just encountered this problem. Interestingly, browsing via http://127.0.0.1 instead of http://localhost helped me.
I just spent all day diagnosing this issue in my Ionic3 - to - PHP project. TL; DR - make sure your client is actually sending session credentials.
In the interest of helping anyone who makes this mistake, I will share how I found the problem.
I used these tools to diagnose the session on both the client and server:
1) Add a test file with phpinfo() to the server to review PHP session options.
2) Review the PHP code to make sure that no output, intentional or un-intentional occurs before the session_start() line. Check the status bar of Visual Studio Code to make sure the Byte Order Mark (BOM) is absent from the PHP files.
3) Review server PHP logs (in /var/log/nginx/error.log for me). Add error_log() lines to the php file to dump the session_id() or $_SESSION array.
4) Use tcpdump -An 'port 80 or port 443' to view the actual HTTP requests and replies. (That's where I discovered the missing cookies).
For an Ionic3 data provider the correct syntax for the client is:
var obsHttp = this.http.post(url, body,
{ headers: new HttpHeaders({
'Content-Type':'application/x-www-form-urlencoded'
}),withCredentials: true }).timeout(this.timeoutTime);
Notice the withCrentials:true
One needs to call subscribe on the obsHttp() observable to send the request.
Found the issue
There was a byte order mark at the beginning of the main includes file of the second domain. as stated by ken, cant have any output before a session start, it was not setting the session correctly.
SOLUTION:
session.auto_start = on
in file: php.ini
It solved the issue of re-generating session id on page reload (page refresh / change pages).
The issue appeared after the update of CPanel (and included Multi PHP), even the php version remained the same.
The PHP.ini file didn't had that variable at all.
Went in Cpanel -> MultiPHP INI Editor -> Editor Mode (not Basic, in basic you do not have this setting) and added the line. Press Save.
TIPS / WHEN TO USE THIS SOLUTION:
To determine if that is the problem, put a line at the very beginning and at the very end of your index.php file to check the session id. Use function:
session_id();
Navigate through pages / reload the page. If the session_id value changes the problem is not in your code and this solution should solve your problem (the session is lost outside of your code).
I also tried to verify the availability of saving session on the web server (session.save_path) but, even if it was a lead, it was not the case.
I imagine this is a "feature" of Cpanel with MULTIPHP UPDATE that will happen quite often.
I had this problem, and the cause was that PHP was ignoring all cookies after the first 100. (I asked this question to try to find out why, but so far nobody has figured it out). The browser was sending the PHPSESSID*, but since it was the 110th cookie, PHP was ignoring it.
To figure out if this problem is what's affecting you, use your browser's dev tools to look at the cookies that the browser is sending with the request, and compare that list to the $_COOKIE array in PHP. They should be the same. But if the browser is sending a PHPSESSID*, and there's no PHPSESSID* in $_COOKIE, then that would explain why sessions aren't working.
I solved the problem by not having my site use so many cookies, which is good practice anyway.
*PHPSESSID is the default session name. Your site may use a different name.
To solve the session_id change after each request, you change the parameter session.auto_start and session.cookie_httponly into the php configuration file.
to find the used php configuration file
php -i | grep "php.ini"
then you open it, and try to find the parameter session.auto_start . you set
session.auto_start = 1
session.cookie_httponly = 0
finally you restart your httpd/apache service.
Found the issue
In my case it was due to Varnish Settings please check your varnish settings. PHPSESSID you can exclude the cookie from the Varnish Settings.
I'm not an expert, but found a solution after careful investigation of domain name in the cookies info of two webpages opened on Firefox. (Right click on the page, select inspection and the storage). checked domain names and found that one with www.example.com and the other without www (example.com). changed all the page links to same format and the problem solved for my case.
Found the problem was a byte order mark (BOM) being ouputted at the start of the file. Got rid of it and it sorted out the session problem.