Table of Contents
How to confirm the values ​​PHP_AUTH_USER and PHP_AUTH_PW in PHP have been set successfully
Home Backend Development PHP Tutorial Setting PHP_AUTH_USER and PHP_AUTH_PW in PHP

Setting PHP_AUTH_USER and PHP_AUTH_PW in PHP

Feb 28, 2024 am 09:04 AM
php programming Backend Development php script

php editor Youzi will introduce to you how to set PHP_AUTH_USER and PHP_AUTH_PW in PHP. These two variables are the username and password used for HTTP basic authentication and can be used to verify user identity. By setting these two variables, you can easily obtain the username and password information provided by the user in your PHP script, thereby achieving secure authentication functionality. This article will explain in detail how to set and get these two variables in PHP code, allowing you to easily deal with authentication needs.


Using curl Post request settings in PHP PHP_AUTH_USER and PHP_AUTH_PW

We will pass to the PHP code Send a curl request to set the username and password.

<code><code class="php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#19177c">$username</span> <span style="color:#666">=</span> <span style="color:#ba2121">'Kevin'</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$pass<strong class="keylink">Word</strong></span> <span style="color:#666">=</span> <span style="color:#ba2121">'Musungu455'</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$url</span> <span style="color:#666">=</span> <span style="color:#ba2121">'<strong class="keylink">Http</strong>://localhost:2145/test2'</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$c</span> <span style="color:#666">=</span> curl_init();
</span></span><span style="display:flex;"><span>curl_setopt(<span style="color:#19177c">$c</span>, CURLOPT_URL, <span style="color:#19177c">$url</span>);
</span></span><span style="display:flex;"><span>curl_setopt(<span style="color:#19177c">$c</span>, CURLOPT_RETURNTRANSFER, <span style="color:#008000;font-weight:bold">true</span>);
</span></span><span style="display:flex;"><span>curl_setopt(<span style="color:#19177c">$c</span>, CURLOPT_USERPWD, <span style="color:#ba2121">"</span><span style="color:#b68;font-weight:bold">$username</span><span style="color:#ba2121">:</span><span style="color:#b68;font-weight:bold">$password</span><span style="color:#ba2121">"</span>);
</span></span><span style="display:flex;"><span>curl_setopt(<span style="color:#19177c">$c</span>, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$output</span> <span style="color:#666">=</span> curl_exec(<span style="color:#19177c">$c</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$info</span> <span style="color:#666">=</span> curl_getinfo(<span style="color:#19177c">$c</span>);
</span></span><span style="display:flex;"><span>print_r(<span style="color:#19177c">$info</span>);
</span></span><span style="display:flex;"><span>curl_close(<span style="color:#19177c">$c</span>);
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
Copy after login

Output:

<code><code class="text hljs" data-lang="text"><span style="display:flex;"><span>Array
</span></span><span style="display:flex;"><span>	(
</span></span><span style="display:flex;"><span>		[url] => http://localhost:2145/test2
</span></span><span style="display:flex;"><span>		[content_type] => text/<strong class="keylink">html</strong>; charset=iso-8859-1
</span></span><span style="display:flex;"><span>		[http_code] => 301
</span></span><span style="display:flex;"><span>		[header_size] => 262
</span></span><span style="display:flex;"><span>		[request_size] => 105
</span></span><span style="display:flex;"><span>		[filetime] => -1
</span></span><span style="display:flex;"><span>		[ssl_verify_result] => 0
</span></span><span style="display:flex;"><span>		[redirect_count] => 0
</span></span><span style="display:flex;"><span>		[total_time] => 0.000658
</span></span><span style="display:flex;"><span>		[namelookup_time] => 0.000132
</span></span><span style="display:flex;"><span>		[connect_time] => 0.000209
</span></span><span style="display:flex;"><span>		[pretransfer_time] => 0.000246
</span></span><span style="display:flex;"><span>		[size_upload] => 0
</span></span><span style="display:flex;"><span>		[size_download] => 236
</span></span><span style="display:flex;"><span>		[speed_download] => 358662
</span></span><span style="display:flex;"><span>		[speed_upload] => 0
</span></span><span style="display:flex;"><span>		[download_content_length] => 236
</span></span><span style="display:flex;"><span>		[upload_content_length] => -1
</span></span><span style="display:flex;"><span>		[starttransfer_time] => 0.000604
</span></span><span style="display:flex;"><span>		[redirect_time] => 0
</span></span><span style="display:flex;"><span>		[redirect_url] => http://localhost:2145/test2/
</span></span><span style="display:flex;"><span>		[primary_ip] => 127.0.0.1
</span></span><span style="display:flex;"><span>		[certinfo] => Array()
</span></span><span style="display:flex;"><span>		[primary_port] => 2145
</span></span><span style="display:flex;"><span>		[local_ip] => 127.0.0.1
</span></span><span style="display:flex;"><span>		[local_port] => 58738
</span></span><span style="display:flex;"><span>		[http_vers<strong class="keylink">io</strong>n] => 2
</span></span><span style="display:flex;"><span>		[protocol] => 1
</span></span><span style="display:flex;"><span>		[ssl_verifyresult] => 0
</span></span><span style="display:flex;"><span>		[scheme] => HTTP
</span></span><span style="display:flex;"><span>	)
</span></span></code></code>
Copy after login

In PHP use curl on the command line to request settings PHP_AUTH_USER and PHP_AUTH_PW

We will send a curl request via the command line to set the username and password.

<code><code class="shell hljs" data-lang="shell"><span style="display:flex;"><span>curl --user Kevin:Musungu455 http://localhost:2145
</span></span></code></code>
Copy after login

How to confirm the values ​​PHP_AUTH_USER and PHP_AUTH_PW in PHP have been set successfully

We will check if the username and password have been set , if set, displays a success message with the username and password.

<code><code class="php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span>(<span style="color:#666">!</span>isset(<span style="color:#19177c">$PHP_AUTH_USER</span>)) {
</span></span><span style="display:flex;"><span>Header(<span style="color:#ba2121">"WWW-Authenticate: Basic realm=</span><span style="color:#b62;font-weight:bold">"</span><span style="color:#ba2121">My Realm</span><span style="color:#b62;font-weight:bold">"</span><span style="color:#ba2121">"</span>);
</span></span><span style="display:flex;"><span>Header(<span style="color:#ba2121">"HTTP/1.0 401 Unauthorized"</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Sign in cancelled</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#ba2121">"</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">exit</span>;
</span></span><span style="display:flex;"><span>} <span style="color:#008000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Hello </span><span style="color:#b68;font-weight:bold">$PHP_AUTH_USER</span><span style="color:#ba2121">.<P>"</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"You entered </span><span style="color:#b68;font-weight:bold">$PHP_AUTH_PW</span><span style="color:#ba2121"> as your password.<P>"</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>
Copy after login

Output:

<code><code class="text hljs" data-lang="text"><span style="display:flex;"><span>Hello Kevin.
</span></span><span style="display:flex;"><span>You entered Musungu455 as your password.
</span></span></code></code>
Copy after login

The above is the detailed content of Setting PHP_AUTH_USER and PHP_AUTH_PW in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP format rows to CSV and write file pointer PHP format rows to CSV and write file pointer Mar 22, 2024 am 09:00 AM

This article will explain in detail how PHP formats rows into CSV and writes file pointers. I think it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Format rows to CSV and write to file pointer Step 1: Open file pointer $file=fopen(&quot;path/to/file.csv&quot;,&quot;w&quot;); Step 2: Convert rows to CSV string using fputcsv( ) function converts rows to CSV strings. The function accepts the following parameters: $file: file pointer $fields: CSV fields as an array $delimiter: field delimiter (optional) $enclosure: field quotes (

PHP changes current umask PHP changes current umask Mar 22, 2024 am 08:41 AM

This article will explain in detail about changing the current umask in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Overview of PHP changing current umask umask is a php function used to set the default file permissions for newly created files and directories. It accepts one argument, which is an octal number representing the permission to block. For example, to prevent write permission on newly created files, you would use 002. Methods of changing umask There are two ways to change the current umask in PHP: Using the umask() function: The umask() function directly changes the current umask. Its syntax is: intumas

The meaning and characteristics of PHP version NTS The meaning and characteristics of PHP version NTS Mar 26, 2024 pm 12:39 PM

PHP is a popular open source scripting language that is widely used in web development. NTS in the PHP version is an important concept. This article will introduce the meaning and characteristics of the PHP version NTS and provide specific code examples. 1. What is PHP version NTS? NTS is a variant of the PHP version officially provided by Zend, which is called NotThreadSafe (non-thread safe). Usually PHP versions are divided into two types: TS (ThreadSafe, thread safety) and NTS

How to read text files in html How to read text files in html Mar 26, 2024 pm 04:07 PM

HTML itself cannot read text files directly, but this functionality can be achieved through back-end programming languages ​​(such as PHP, Python, Java) or front-end JavaScript technology. The backend method uses PHP's file_get_contents() function to read the content from the text file and embed it into the HTML page. The front-end JavaScript method uses the Fetch API to send a GET request to a text file on the server, then parses the response content and displays it in an HTML page.

php suite linux PHP suite carnival! Combining two swords under Linux, development efficiency soars php suite linux PHP suite carnival! Combining two swords under Linux, development efficiency soars Mar 30, 2024 pm 12:21 PM

Recently, the industry has generally paid great attention to the application of PHP software suites in Linux operating systems. As today's most popular server-side scripting language, PHP has a wide range of applications in the field of Web development. The Linux system has become the first choice for the majority of users due to its stable performance, high security and complete openness. This article aims to discuss in detail the actual application of the PHP software suite in the Linux system environment and its maximum integration effect. 1. Introduction to PHP suite The so-called PHP suite is essentially a comprehensive tool component that facilitates programmers to easily complete related program tasks, reduces the complexity of code development, and thereby improves development efficiency. Take Larv

Steps and precautions for modifying encoding settings in PHP.ini Steps and precautions for modifying encoding settings in PHP.ini Mar 27, 2024 pm 06:06 PM

Steps and precautions for modifying encoding settings in PHP.ini PHP is a powerful server-side scripting language that is widely used in the field of web development. In the PHP development process, it is often necessary to process data in different encoding formats, so it is very important to set the encoding correctly. This article will introduce how to set the encoding by modifying the PHP configuration file php.ini, and provide specific code examples. Step 1: Locate the php.ini configuration file. First, you need to locate the php.ini configuration file in the PHP installation directory.

How to install PHP FFmpeg extension on server? How to install PHP FFmpeg extension on server? Mar 28, 2024 pm 02:39 PM

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe

PHP returns the numeric encoding of the error message in the previous MySQL operation PHP returns the numeric encoding of the error message in the previous MySQL operation Mar 22, 2024 pm 12:31 PM

This article will explain in detail the numerical encoding of the error message returned by PHP in the previous Mysql operation. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. . Using PHP to return MySQL error information Numeric Encoding Introduction When processing mysql queries, you may encounter errors. In order to handle these errors effectively, it is crucial to understand the numerical encoding of error messages. This article will guide you to use php to obtain the numerical encoding of Mysql error messages. Method of obtaining the numerical encoding of error information 1. mysqli_errno() The mysqli_errno() function returns the most recent error number of the current MySQL connection. The syntax is as follows: $erro

See all articles