PHP cURL: Authentication: Difference between revisions

Jump to navigation Jump to search
m
Reverted edits by Upazixorys (Talk); changed back to last version by Ric
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric)
 
Line 1: Line 1:
=[http://awibuky.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
{{Nav PHP cURL}}
{{Nav PHP cURL}}
'''Validation Servrs'''
'''Validation Servrs'''
Line 15: Line 14:


Change these four lines:
Change these four lines:
<pre>
<pre>
#AuthName &quot;Uniform Server - Server Access&quot;
#AuthName "Uniform Server - Server Access"
#AuthType Basic
#AuthType Basic
#AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd
#AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd
#Require valid-user
#Require valid-user
&lt;/pre&gt;
</pre>
To:
To:
&lt;pre&gt;
<pre>
AuthName &quot;Uniform Server - Server Access&quot;
AuthName "Uniform Server - Server Access"
AuthType Basic
AuthType Basic
AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd
AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd
Require valid-user
Require valid-user
&lt;/pre&gt;
</pre>
'''''Quick test'':'''
'''''Quick test'':'''


Type '''&lt;nowiki&gt;http&lt;/nowiki&gt;://localhost:82/''' into your browser, when challenged for a name and password press '''cancel'''.  
Type '''<nowiki>http</nowiki>://localhost:82/''' into your browser, when challenged for a name and password press '''cancel'''.  


A page is displayed with something like Authorization Required, this confirms authentication is enabled.
A page is displayed with something like Authorization Required, this confirms authentication is enabled.
Line 38: Line 37:
== Example 5 - Download and display a page ==
== Example 5 - Download and display a page ==
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test5.php''' add the following content
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test5.php''' add the following content
{|cellspacing=&quot;6&quot;
{|cellspacing="6"
|-
|-
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
  $ch=curl_init();
  $ch=curl_init();
  curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php');
  curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php');
  curl_exec($ch);
  curl_exec($ch);
  curl_close($ch);
  curl_close($ch);
?&gt;
?>
&lt;/pre&gt;
</pre>
|}
|}
'''''Test'':'''
'''''Test'':'''
* Run both servers
* Run both servers
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//localhost/test5.php''' into your browser
* Type '''<nowiki>http:</nowiki>//localhost/test5.php''' into your browser
* Result: Page displayed as follows
* Result: Page displayed as follows
&lt;pre&gt;
<pre>
Authorization Required
Authorization Required


Line 61: Line 60:
(e.g., bad password), or your browser doesn't understand how to
(e.g., bad password), or your browser doesn't understand how to
supply the credentials required.
supply the credentials required.
&lt;/pre&gt;
</pre>
The above proves our servers are set-up and working correctly.
The above proves our servers are set-up and working correctly.


Line 72: Line 71:
A name and password is passed to Curl using the following function:
A name and password is passed to Curl using the following function:


* '''curl_setopt($ch, CURLOPT_USERPWD, &quot;myusername:mypassword&quot;)'''  
* '''curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword")'''  


Our test server curl_2 uses Uniform Server's defaults name='''root''' password='''root '''
Our test server curl_2 uses Uniform Server's defaults name='''root''' password='''root '''


Modify file C:\curl_1\UniServer\www\'''test5.php'''
Modify file C:\curl_1\UniServer\www\'''test5.php'''
{|cellspacing=&quot;6&quot;
{|cellspacing="6"
|-
|-
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
  $ch=curl_init();
  $ch=curl_init();
  curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php');
  curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php');
curl_setopt($ch, CURLOPT_USERPWD, &quot;root:root&quot;);
curl_setopt($ch, CURLOPT_USERPWD, "root:root");
  curl_exec($ch);
  curl_exec($ch);
  curl_close($ch);
  curl_close($ch);
?&gt;
?>
&lt;/pre&gt;
</pre>
|}
|}
'''''Test'':'''
'''''Test'':'''
* Run both servers
* Run both servers
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//localhost/test5.php''' into your browser
* Type '''<nowiki>http:</nowiki>//localhost/test5.php''' into your browser
* Result: '''Your IP is 127.0.0.1''' - displayed
* Result: '''Your IP is 127.0.0.1''' - displayed


Line 107: Line 106:


Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test6.php''' add the following content
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test6.php''' add the following content
{|cellspacing=&quot;6&quot;
{|cellspacing="6"
|-
|-
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
$ch=curl_init();
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php');
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php');
curl_setopt($ch, CURLOPT_USERPWD, &quot;root:root&quot;);
curl_setopt($ch, CURLOPT_USERPWD, "root:root");
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
Line 121: Line 120:


if (empty($buffer)){
if (empty($buffer)){
   print &quot;Need to recover from this!&lt;br /&gt;&quot;;
   print "Need to recover from this!<br />";
}
}


else{
else{
   print &quot;There was data returned using curl.&lt;br /&gt;&quot;;
   print "There was data returned using curl.<br />";
   print &quot;Buffer content = &quot;.$buffer.&quot;&lt;br /&gt;&quot;;
   print "Buffer content = ".$buffer."<br />";


   // Extract IP address  
   // Extract IP address  
  if(preg_match(&quot;/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/&quot;, $buffer, $ipmatch)){   
  if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){   
     $ip = $ipmatch[0]; // Save IP to variable
     $ip = $ipmatch[0]; // Save IP to variable
     print $ip;                                 
     print $ip;                                 
  }
  }
}
}
?&gt;
?>
&lt;/pre&gt;
</pre>
|}
|}
'''''Test'':'''
'''''Test'':'''
* Run servers
* Run servers
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//localhost/test6.php''' into your browser
* Type '''<nowiki>http:</nowiki>//localhost/test6.php''' into your browser
* Result:
* Result:
&lt;pre&gt;
<pre>
There was data returned using curl.
There was data returned using curl.
Buffer content = Your IP is 127.0.0.1
Buffer content = Your IP is 127.0.0.1
127.0.0.1
127.0.0.1
&lt;/pre&gt;
</pre>


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''

Navigation menu