Définition de PHP_AUTH_USER et PHP_AUTH_PW en PHP

WBOY
Libérer: 2024-02-28 09:06:01
avant
1124 Les gens l'ont consulté

L'éditeur php Youzi vous présentera comment définir PHP_AUTH_USER et PHP_AUTH_PW en PHP. Ces deux variables sont le nom d'utilisateur et le mot de passe utilisés pour l'authentification de base HTTP et peuvent être utilisées pour vérifier l'identité de l'utilisateur. En définissant ces deux variables, vous pouvez facilement obtenir les informations de nom d'utilisateur et de mot de passe fournies par l'utilisateur dans votre script PHP, implémentant ainsi une fonction d'authentification sécurisée. Cet article expliquera en détail comment définir et obtenir ces deux variables dans le code PHP, vous permettant de gérer facilement les besoins d'authentification.


Utilisation de curl dans les paramètres de requête PHP Post PHP_AUTH_USER et PHP_AUTH_PWcurl Post 请求设置 PHP_AUTH_USERPHP_AUTH_PW

我们将通过向 PHP 代码发送 curl 请求来设置用户名和密码。

<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>
Copier après la connexion

输出:

<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>
Copier après la connexion

在 PHP 中在命令行中使用 curl 请求设置 PHP_AUTH_USERPHP_AUTH_PW

我们将通过命令行发送 curl 请求来设置用户名和密码。

<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>
Copier après la connexion

在 PHP 中如何确认值 PHP_AUTH_USERPHP_AUTH_PW

Nous enverrons curlau code PHP > demande de définition du nom d'utilisateur et du mot de passe.

<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>"</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>
Copier après la connexion
Sortie :

<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>
Copier après la connexion

-in-php-in-command-line-in-curl- request-settings-php_auth_pw">in-php-in-command-line-in-php-using-curl-request-settings-code> Demande de définition de PHP_AUTH_USER et PHP_AUTH_PW🎜🎜Nous enverrons une requête curl via la ligne de commande pour définir le nom d'utilisateur et le mot de passe. 🎜rrreee🎜

Comment confirmer les valeurs ​​​​PHP_AUTH_USER et PHP_AUTH_PW en code PHP> A-t-il été configuré avec succès ? 🎜🎜 Nous vérifierons si le nom d'utilisateur et le mot de passe ont été définis et si oui, afficherons un message de réussite avec le nom d'utilisateur et le mot de passe. 🎜rrreee🎜Sortie : 🎜rrreee

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:lsjlt.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal