search
HomeBackend DevelopmentPHP TutorialSetting 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 programmingBackend Developmentphp 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="language-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>

Output:

<code><code class="language-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>

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="language-shell hljs" data-lang="shell"><span style="display:flex;"><span>curl --user Kevin:Musungu455 http://localhost:2145
</span></span></code></code>

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="language-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>"</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>"</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>

Output:

<code><code class="language-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>

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
This article is reproduced at:编程网. If there is any infringement, please contact admin@php.cn delete
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function