My header redirect is not working on the live server - at least 1/2 of it isn't

Hi. I’m learning to program PHP more as a hobby than anything. I’ve just joined SitePoint and this is my first (but I’m sure not last) request for assistance. And thanks in advance for any assistance.

I am working on a web site where I want a page limited to authorized individuals. When someone accesses the page (index.php file), if they have not signed in they are redirected to a login form. This redirect is working fine.

After entering their username and password, the form should redirect them back to the index.php. This works on my local server but on the live server, the login form seems to just reload but doesn’t even re-display the form. Just a blank page.

On the index.php page, I have the following:

// If an authorized user is accessing, get their access authorization level and name
    if($loginAuthorized){
        $accessLevel=$_COOKIE['accessLevel'];
        $fullname=$_COOKIE['fullname'];
        echo 'Name is: '.$fullname.'<br />';
    }else{
        header("Location: _forms/loginform.php");
    }

As I mentioned, that works and sends the user to the login form. On the login form I have the following:

//Check whether password retreived matches one input
        if ($password==$pwordRetrieved){
            //success
           $fullname=$fnameRetrieved.' '.$lnameRetrieved;
            setcookie("loginAuthorized", "loginAuthorized", 0,"/");
            setcookie("accessLevel", $accessLevelRetrieved, 0,"/");
            setcookie("fullname", $fullname,0,"/");
            
            header("Location: ../index.php");
            exit();
            
        }else{
            //Not success
            echo 'Incorrect user name and/or password<br />';
            echo '<a href='.$thisScriptName.'>Try again</a>';
            exit;
        }

This one doesn’t work. As I mentioned, both redirects work fine on my local server.

If it is relevant, my local server is running PHP version 7.4.2. The hosting company for the live server is running PHP version 5.3.29.

Any ideas?

Hi @wpwheeler54 and a warm welcome to the forum.

I’m guessing that if it works ok locally and not on the remote server the problem is most likely to be file name case sensitivity. Apache2 is most likely to be the online operating system and file name existence can be checked using the following:

<?php 
error_reporting(-1); // maximum error reporting
ini_set('display_errors', '1'); // show errors in browser

$fileName = 'loginForm.php';
if(file_exists($fileName)) {
  // echo '<br>no problem';
} else {
  echo '<br> missing file: ' .$fileName;
  echo '<br>current directory: ' .getcwd();
  die; // halt display browser output
}

That is hundreds upon hundreds of releases behind. You need to upgrade or find a new host. Even for an old legacy app it is way behind.

So what is the form code?

Thanks for the suggestion. I never knew how to enable the PHP error reporting.

Based on the error messages I am seeing, it appears that setting the cookies before trying to redirect is preventing the redirect from working. I will work on removing the cookies (possibly using session variables instead?) and also possibly making this a single page rather than redirecting back and forth. I will let you know if either (or both) of those work.

1 Like

I forgot to mention about using the following declaration with PHP 7 which I fid very useful in highlighting errors:

<?php 
declare(strict_types=1); 

The documentation suggests that the setcookie() function just prepares the cookie headers, it doesn’t actually send them, which would further suggest that it wouldn’t interfere with the header redirect. What error messages are you seeing?

Apparently the hosting company does not upgrade the PHP version for your domain. You need to manually do that. I guess they don’t want to be responsible for causing your script to not work.

Anyway, realizing that I changed my domain to run with the latest they had which was 7.3.

2 Likes

Glad to hear it. So are you still having a problem? If you are, PM me the code and I will take a look at it.

Sorry I haven’t followed up for awhile. Been busy (work often gets in the way of pleasure).

Thanks for the tips. In particular, getting the error messages printed to the screen.

I have resolved my issue by not using cookies but saving all necessary data as session variables.

Again, thanks.

1 Like