Authentication does not work on PHP 4.X

Discussion in 'PHP' started by ignas2526, Jan 23, 2009.

  1. #1
    Hello,
    I trying to figure out why this code does not work on PHP 4.4.7:
    if(!isset($_SERVER['PHP_AUTH_USER'])||!isset($_SERVER['PHP_AUTH_PW'])||$_SERVER['PHP_AUTH_USER']!='user'||$_SERVER['PHP_AUTH_PW']!='pass'){
    	header('WWW-Authenticate: Basic realm="Realm"');
    	header('HTTP/1.0 401 Unauthorized');
    	die("Access Denied");
    	}
    	echo 'passed';
    }
    PHP:
    This code works fine on PHP 5.2.6 but on PHP 4.4.7 it keeps asking again and again for username and password even if I entered correct.
    Any ideas what is wrong?
    Thanks.
     
    ignas2526, Jan 23, 2009 IP
  2. Panzer

    Panzer Active Member

    Messages:
    381
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Regardless, you should switch to PHP 5. PHP 4 is very insecure, isn't being updated any more and misses out on some powerful features included in 5.
     
    Panzer, Jan 23, 2009 IP
  3. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So are there any other ways instead of switching to PHP 5?
     
    ignas2526, Jan 24, 2009 IP
  4. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You should really just use sessions; once someone logs in, you can set a session variable:

    $_SESSION['uid'] = $user_id;

    and then check to see if it is set on any page that is password protected:

    if (!isset($_SESSION['uid'])) {

    $current_page = basename($_SERVER['PHP_SELF']);
    $location = "login.php?go=" . $current_page;
    header("location: $location");

    }
     
    GreatMetro, Jan 24, 2009 IP
  5. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Don't forget, while using sessions, the file MUST start with session_start(); (outside of comments) for them to properly work. For instance, you can do this manually per each file or you can set some sort of initiating file. E.g.:

    init.php
    <?php
    /**
     * Blah
     * Blah
     * Your comments
     * herE!
     *
     */
    session_start();
    
    define("ROOT_PATH", dirname(__FILE__)); // Define script root directory [where this file should be :)]
    define("INIT_SUCCESS",true); // If this doesn't come up, your script didn't load correctly.
    
    ?>
    
    PHP:
    index.php
    
    <?php
    /**
     * Comments again...
     * 
     * Let's load the next file BEFORE anything else to get our session_start(); :)
     *
     */
    require_once("init.php");
    
    if(!defined("INIT_SUCCESS")){ die("Your init.php did not load properly..."); } else { echo("No errors? Success!<br /><br />Your root path is: ".ROOT_PATH); exit; }
    
    ?>
    
    PHP:
    Regards,
    Dennis M.
     
    Dennis M., Jan 24, 2009 IP
  6. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Obviously. Just make sure you don't have any whitespace before you start your session or output headers. PHP 101.
     
    GreatMetro, Jan 24, 2009 IP