Explain how to do user authentication in php.
The most convenient way to authenticate a user in PHP is to use session. Another way is to use HTTP authentication:-
<?php
if (!isset($_SERVER['PHP_AUTH_USER']))
{
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
}
else
{
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>
Explain how to do user authentication in php.
For user authentication, GD library is needed and is bundled with PHP. One needs to enable the GD support before utilizing the library.
To view the GD library, execute the following code.
<?php
if (function_exists('imagecreate'))
{
echo "GD Library is enabled <br>\r\n<pre>";
var_dump(gd_info());
echo "</pre>";
}
else
{
echo 'Sorry, you need to enable GD library first';
}
?>
If GD is disabled, then few changes need to be done in php.ini file. Find for extension=php_gd2.dll. Remove the semicolon to uncomment the script line. Restart the web server and run the test script. GD enabled message will be echoed. This confirmation message ensures the user authentication.