S
S
sunnyrio2016-10-19 20:36:09
PHP
sunnyrio, 2016-10-19 20:36:09

Why doesn't a window appear prompting you to enter a username and password when starting the php program in the browser?

When you run the php program in the browser, a window does not appear asking you to enter a username and password. After the first incorrect entry into the authorization window, the program does not re-issue a window asking you to enter a username and password, but only writes a message stating that an incorrect username or password was entered, what's the matter, how to return the authorization window?
Here is the code itself from O'reilly's book:

<?php // authenticate.php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Невозможно подключиться к MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Невозможно выбрать базу данных: " . mysql_error());
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW']))
{
$un_temp = mysql_entities_fix_string($_SERVER['PHP_AUTH_USER']);
$pw_temp = mysql_entities_fix_string($_SERVER['PHP_AUTH_PW']);

$query = "SELECT * FROM users WHERE username='$un_temp'";
$result = mysql_query($query);
if (!$result) die("Сбой при доступе к базе данных: " . mysql_error());
elseif (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
$salt1 = "qm&h*";
$salt2 = "[email protected]";
$token = md5("$salt1$pw_temp$salt2");

if ($token == $row[3]) echo "$row[0] $row[1]:
Привет, $row[0], теперь вы зарегистрированы под именем '$row[2]'";
else die("Неверная комбинация имя пользователя-пароль");
}
else die("неверная комбинация имя пользователя-пароль");
}
else
{
header('WWW-Authenticate: Basic realm="Restricted Section"');
header('HTTP/1.0401 Unauthorized');
die ("Пожайлуста, введите имя пользователя и пароль");
}

function mysql_entities_fix_string($string)
{
return htmlentities(mysql_fix_string($string));
}

function mysql_fix_string($string)
{
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return mysql_real_escape_string($string);
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2016-10-19
@sunnyrio

Be especially careful when specifying HTTP headers. In order to guarantee maximum compatibility with the largest number of different clients, the word "Basic" must be capitalized "B", the region (realm) must be enclosed in double (not single!) quotes, and exactly one space must precede the code 401 in the HTTP/1.0 401 header .
php.net/manual/en/features.http-auth.php

M
Maxim, 2016-10-19
@pudovMaxim

if ($token == $row[3]) echo "$row[0] $row[1]:
Привет, $row[0], теперь вы зарегистрированы под именем '$row[2]'";
else die("Неверная комбинация имя пользователя-пароль");
}
else die("неверная комбинация имя пользователя-пароль");
}
else
{

Arrange the brackets and take a closer look at what's what

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question