Jump to content

auth.php / password protected website


moechyman

Recommended Posts

I have used the code below to try and password protect a website, but I can't get it to work. Where did I go wrong?

 

 

 

<?php

 

$login_successful==true;

 

// check user & pwd:

if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){

 

    $usr == $_SERVER['PHP_AUTH_USER'];

    $pwd == $_SERVER['PHP_AUTH_PW'];

 

    if ($usr == ' ' && $pwd == 'murphy'){

    }

}

 

// login ok?

$login_successful==false;

if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){

 

if ($usr != ' ' && $pwd != 'murphy'){

    //send 401 headers:

//realm="something";

    header('WWW-Authenticate: Basic realm=Sorry please try again');

    header('HTTP/1.0 401 Unauthorized');

print("Login failed!");

 

 

}

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/153644-authphp-password-protected-website/
Share on other sites

You have a misunderstanding of using the single equals and double equals. Furthermore, your logic is flawed.

 

1 equal - Assigns variables

$assign1 = $_POST['username'];
$assign2 = 'Hello';

//Prints a username grabbed from another page
echo $assign1;

//Prints hello
echo $assign2;

 

2 equals - Checks to see if the variable matches

$user1 = 'Ben';
$user2 = 'Fin'
$user3 = 'Ben';

//Will echo They dont match!
if ($user1 == $user2)
  echo 'They match!';
else
  echo 'They dont match!';

//Will echo They match!
if ($user1 == $user3)
  echo 'They match!';
else
  echo 'They dont match!';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.