<?php $url ="http://example.com/target.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_USERPWD,'username:password'); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); ?>
On the server side (target.php), we can access the username and password as follows.
$USERNAME = $_SERVER['PHP_AUTH_USER']; $PASSWORD = $_SERVER['PHP_AUTH_PW'];
In some cases, the $ _SERVER variables are NOT AVAILABLE to YOUR LOCAL SERVER. SO PLEASE NEXT CODE IN YOUR TENSION. HTTP AUTHORIZATION MODULE WILL WORK IN PHP. REASON - THIS WE CAN INSTALL THIS AS A SEPARATE MODULE
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] </IfModule>
user1099325
source share