Setting PHP_AUTH_USER and PHP_AUTH_PW in PHP
curl
This article will show you how to use Requests in PHP to set PHP_AUTH_USER
and PHP_AUTH_PW
and use Requests through the command line curl
. It will also show you how to confirm that the values of PHP_AUTH_USER
and PHP_AUTH_PW
have been successfully set.
curl
Using Post request in PHP to set PHP_AUTH_USER
andPHP_AUTH_PW
curl
We will set the username and password by sending a request to the PHP code .
<?php
$username = 'Kevin';
$password = 'Musungu455';
$url = 'http://localhost:2145/test2';
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, "$username:$password");
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($c);
$info = curl_getinfo($c);
print_r($info);
curl_close($c);
?>
Output:
Array
(
[url] => http://localhost:2145/test2
[content_type] => text/html; charset=iso-8859-1
[http_code] => 301
[header_size] => 262
[request_size] => 105
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.000658
[namelookup_time] => 0.000132
[connect_time] => 0.000209
[pretransfer_time] => 0.000246
[size_upload] => 0
[size_download] => 236
[speed_download] => 358662
[speed_upload] => 0
[download_content_length] => 236
[upload_content_length] => -1
[starttransfer_time] => 0.000604
[redirect_time] => 0
[redirect_url] => http://localhost:2145/test2/
[primary_ip] => 127.0.0.1
[certinfo] => Array()
[primary_port] => 2145
[local_ip] => 127.0.0.1
[local_port] => 58738
[http_version] => 2
[protocol] => 1
[ssl_verifyresult] => 0
[scheme] => HTTP
)
In PHP, use curl
the request settings PHP_AUTH_USER
andPHP_AUTH_PW
We will send a request through the command line curl
to set the username and password.
curl --user Kevin:Musungu455 http://localhost:2145
PHP_AUTH_USER
How to confirm whether the value and PHP_AUTH_PW
is successfully set in PHP
We will check if the username and password are already set and if so, display a success message with the username and password.
<?php
if(!isset($PHP_AUTH_USER)) {
Header("WWW-Authenticate: Basic realm="My Realm"");
Header("HTTP/1.0 401 Unauthorized");
echo "Sign in cancelled\n";
exit;
} else {
echo "Hello $PHP_AUTH_USER.<P>";
echo "You entered $PHP_AUTH_PW as your password.<P>";
}
?>
Output:
Hello Kevin.
You entered Musungu455 as your password.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of