2

After years of developing websites and in this case, the same website I'm having a problem with, I'm at a complete loss with what's happening. I have a standard php login that consists of three pages:

login.php to input login information, checklogin.php to check the input credentials then home.php as the landing page

All of which have worked perfectly fine for years as they've barely been changed within that time. Now, recently I've moved hosting company and with that obviously meant the daunting task of re-installing all of my preferred web hosting software and configuring it to my websites needs. So anyway since the website coding hasn't changed, I've figured it must be a server problem, either the server configuration or a setting I must have missed out.

The configuration set up I have now worked for all of three days then 'poof' it just doesn't want to set sessions any more. I can set one and display it but if I try to carry it over to another page, it won't work.

Can anyone explain what may have just magically happened within three days that decided to just ruin everything?

A few notes:

  • Yes, I am using session_start(); on my landing page and any I intend to take the sessions to.
  • I've used the tags to give you an idea of what software my server is running for web pages (Since I'm still unsure what's causing it)
  • The php session path is /var/lib/php/sessions/ which has the correct permissions for Apache/nginx
  • MySQL is collecting the data and php is setting the sessions on page 2 but then page 3 seems to have lost the sessions.

Here's an example of a quick throw-together I did to test it:

Page 1: Setting the sessions:

<?php
session_start();
require_once'../connect.php'; // Database connection
$user_info = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id = '1'"));

$_SESSION['myid'] = $user_info['id'];
$_SESSION['myuser'] = $user_info['username'];

// Echo the set sessions just to make sure they set

echo 'ID: ' . $_SESSION['myid'] . ' - Username: ' . $_SESSION['myuser'] . '';
echo '<br /><a href="/page2.php">Click here to take your sessions to the next page</a>';
?>

Page 2: Moving on with the sessions:

<?php
session_start();
echo 'ID: ' . $_SESSION['myid'] . ' - Username: ' . $_SESSION['myuser'] . '';
?>

As you can see from the basic example above, page 2 should display my sessions but it just won't. Any help with this is much appreciated, it's driving me mad, mainly for the fact of not knowing what's causing it. Let me know if any further details are needed and I'll try to be as specific as possible.

SmurfTheSmurf
  • 105
  • 2
  • 12
  • Do you get any erros or just nothing when your trying to display Session variables on page 2? (Be sure error reporting to turn on) Also make sure the tmp folder have space left and you have permission to write! – Rizier123 Nov 13 '14 at 00:44
  • 2
    Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Nov 13 '14 at 00:45
  • Alright, now I'm getting an error I didn't before on the two test pages I created: `Warning: Unknown: write failed: No space left on device (28) in Unknown on line 0` – SmurfTheSmurf Nov 13 '14 at 00:52
  • @Equinox04 i think your tmp folder is full? – Rizier123 Nov 13 '14 at 01:03
  • This may help http://stackoverflow.com/questions/5412435/what-does-write-failed-no-space-left-on-device-mean – RiggsFolly Nov 13 '14 at 01:06
  • @RiggsFolly I had a look at that before posting - I don't use shared hosting and my `/tmp/` folder isn't full as well as having the correct permissions. – SmurfTheSmurf Nov 13 '14 at 01:14
  • @Equinox04 But your warning means your device is full and you cannot write in a file! (Check if you have enought space on your folders where you save stuff) Also check if your backuping any data/ session/ server or tmp folder and if the backup files/ folder are too big! – Rizier123 Nov 13 '14 at 01:21

3 Answers3

1

First thing todo in such a situation is to turn on error reporting with this:

<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>

After that i would guess you don't have write permission on your tmp folder or the tmp folder is full!

So check your permissions on the folder and if it has space left.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • I've managed to get it fixed on one server, the `/tmp/` folder had a 14gb backup of the database. Ha. Anyway, my two other servers still seem to want to be stubborn. I've put those two test pages on them and got this error now: `Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0` The path is correct and all three servers have the same php.ini settings. It also has the correct permissions. – SmurfTheSmurf Nov 13 '14 at 01:45
  • @Equinox04 hmm okay! But the hint with the backup was good ? wasn't it?;D – Rizier123 Nov 13 '14 at 01:56
  • Haha, yes it was, thank you :) One fixed, two to go. – SmurfTheSmurf Nov 13 '14 at 01:58
  • @Equinox04 Just out of curiosity: You only have 3 servers right? Or are there more servers to fix :D? – Rizier123 Nov 13 '14 at 01:59
  • 1
    @Equinox04 Do you have a folder `/var/tmp` Because if this folder is full it could affect saving session data! – Rizier123 Nov 13 '14 at 02:03
  • I do indeed, that was also full on one of the servers and fixed the problem, now it's just the third one which has both of those tmp folders empty already... – SmurfTheSmurf Nov 13 '14 at 02:13
  • @Equinox04 Lol Okay :D just to be clear: You have 3 servers and now 2 server works and 1 still doesn't work? Which errors do you get on your last server? (BTW: It's a shame that you cant accept 1 answer 3 times for ever server xD) – Rizier123 Nov 13 '14 at 02:15
  • Yes, 3 servers, 2 seem to be running fine now, one still has a write error for sessions. – SmurfTheSmurf Nov 13 '14 at 02:19
  • @Equinox04 on your last server you checked everything? permission, backup folders, space of the folder, tmp folder and so on? Also a reboot is always good! Then check your php.ini file that you don't have a different one or even multiple on the server! And check that the life time of a session is set! – Rizier123 Nov 13 '14 at 02:23
  • Well I'm back to square one with the other two servers again now anyway. I'm getting the same error all over again after only 10 minutes of the website being back online. I've checked the `/tmp/` folders and there's nothing in either of them any more. – SmurfTheSmurf Nov 13 '14 at 02:27
  • @Equinox04 i would contact the host provider if possible? Or try to delete all session you have saved at this time and rebuild the tmp folders and set the permissions again if you can. Also make sure in your php.ini file the session.save_path is set correctly or try to set the full path! – Rizier123 Nov 13 '14 at 02:27
  • Looks like I may have to. I tried to avoid doing so because they aren't the easiest people to talk to. Oh well, needs must. – SmurfTheSmurf Nov 13 '14 at 02:31
  • @Equinox04 You can try my ideas from the last comment! But after that im out of ideas! sry – Rizier123 Nov 13 '14 at 02:33
1

Well I finally worked out where I was going wrong so I'll explain for anyone who runs into the same problem.

Having the PHP sessions stored in /var/lib/php/session was my main problem, not because of read/write permissions but because the partition of the / directory was absolutely tiny and filled with logs.

A quick fix would be to just delete the logs and anything else cluttering the directory but I personally went with moving the session storing directory to a bigger partition: /home

You can do this in your php.ini file which will probably be set to either /tmp or /var/lib/php/session

; session.save_path = "/home"

You will need to restart any php service and/or web server you have running to reload php.ini, in my case was nginx and php-fpm

$ service nginx restart
$ service php-fpm restart

(Depending on your OS) You can check how big your partitions are on your server with:

$ df -h

Which should give you something like this:

> df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  6.1G   41G  13% /
tmpfs                  12G     0   12G   0% /dev/shm
/dev/sdb1             477M   60M  392M  14% /boot
/dev/mapper/VolGroup-lv_home
                      409G   71M  388G   1% /home

So just choose your preferred partition and make it your session path in php.ini

Thanks to everyone who commented trying to help solve my problem :)

SmurfTheSmurf
  • 105
  • 2
  • 12
  • nice that you figurt it out! (BTW: you can accept your own answer!, also a upvote would be nice for my aeffort) – Rizier123 Nov 13 '14 at 04:28
0

What version of PHP are your servers running? I recently lost login authentication because of the upgrade to PHP v5.4(?not sure).

Basically, session_is_registered is depricated and gone. I switched to validating a session identity:

if(isset($_SESSION['valid_user']))

This may not be the most secure adaptation, but it worked for my low security website.

Kirk Powell
  • 908
  • 9
  • 14
  • I run php 5.3 on all three servers. However, in regards to what you've suggested, I've never used the `session_is_registered();` function. I've always gone with how you do it now for behind login page autentication. `if(!isset($_SESSION['logged_in'])){ // Return to login }else{ // Show page }` Probably not the most efficient way but it does what I need it to. – SmurfTheSmurf Nov 13 '14 at 02:02