Authentication in headers through credentials instead of cookie - php

I am accessing a url which needs authentication, so for authentication i am using cookie something like this
$out = "GET $path HTTP/1.0\r\nHost: $host\r\nCookie: user=$cookie\r\n\r\n";
$fp = fsockopen($host, 80, $errno, $errstr, 30) or htmlerror('Bandwidth limit exceeded, please try again later.');
fwrite($fp, $out);
so i wanted to know whether instead of cookie how can i use directly username and password in variable $out
Thank You

I had a quick view on HTTP GET REQUEST, you might try to insert something into the query part of the URI
$out = 'GET /*?*username=$user&password=$encryptedPW HTTP/1.1'
$out .= '/n' . 'Host: $host'

it didnt work, just using it with cookie

Related

HTTP 400 BAD REQUEST error while sending GET request with php

While I'm trying to send a GET request to an address with php I receive HTTP 400 BAD REQUEST message and I can't figure out why.
Here's my code:
function redirect($url)
{
error_reporting(E_ERROR | E_PARSE);
$components = parse_url($url);
$port = $components["port"];
$ip = $components["host"];
$path = $components["path"];
//create and connect socket with the parameters entered by the user
//$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
echo "Establishing connection to the given adress...\n";
//Connection timeout limit is set to 10 seconds...
if(!isset($port))
{
$port = 80;
}
$sock = fsockopen($ip, $port,$errno, $errstr, 10) or die("Unable to connect...");
$request = "GET $path HTTP/1.1" . "\r\n\r\n";
fwrite($sock, $request);
while ($header = stream_get_line($sock, 1024, "\r\n")) {
$response.= $header . "\n";
}
echo $response;
$loc = "";
if (preg_match("/Location:\'(.*)\\n/", $response, $results))
$loc = $results[1];
echo $loc;
}
Any suggestions?
A GET request also contains a header wich include things like the useragent oder the encoding, you should have a look at what you need to send and what's optional
For this specific problem I've found a solution. If you want to get the headers from the request that I sent in the question above, you can use php's get_headers($url) method. It retrieves the headers that I was looking for. I'm really new to protocols and web-services and also to php (1 or 1.5 weeks), therefore I may have asked a silly question and not have been specific enough. Anyways thank you very much for your answers.
Have a nice day!

How to get the response from URL without cURL?

I am working on twitter login integration with website. I don't have cURL installed in my server and I am not allowed to install that.
Twitter code is working fine for login. But while using request_token curl is used to send callback URL with that URL and getting the token response. In this same case I want to get the response from that URL without using Curl in PHP. Is it possible?
Curl code now used:
$response = curl_exec($ci);
The above response I need without using Curl.
How to get the response from url without Curl
You don't have to necessarily use cURL, there can be many ways. One of those is:
$response=file_get_contents($ci);
Edit:
You can also use fsockopen, here is an example from PHP.net
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>

Posting from PHP to PHP

I am working on creating a system which is secured by passing tokens from page to page to verify the validity of requests. The token is to be generated on the login page (as it only needs to be generated once) and then passed through to the main page. However, there is an intermediate PHP script which is being run to log the user in, and I am not sure how to take the posted token and pass it on to the main page.
In short, I need to post the token from the login page to the intermediary script, and then from the script to the main page, and I'm not sure how to do that.
If all 3 pages are on the same host, use a cookie or a session.
Store the data in a cookie/session instead:
session_start();
$_SESSION['token'] = 'yourTokenHere'
See PHP Sessions
I wrote this a long time ago:
function postheader($url, $server, $cookies, $daten)
{
$temp=array();
$out = "POST ".$url." HTTP/1.1\r\n";
$out .= "Host: ".$server."\r\n";
if(count($cookies)>0)
{
$out .= "Cookie: ";
foreach($cookies as $name=>$value)
{
$temp[] = $name."=".$value;
}
$out .= implode("; ",$temp);
$out .= "\r\n";
}
$out .= "User-Agent: Mozilla/4.0\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$temp=array();
foreach($daten as $key=>$data)
{
$temp[] = $key."=".urlencode($data);
}
$temp=implode("&",$temp);
$out .= "Content-Length: ".strlen($temp)."\r\n";
$out .= "\r\n";
$out .= $temp."\r\n";
return $out;
}
$fp = fsockopen($server, 80, $errno, $errstr, 30);
if (!$fp)
die( "$errstr ($errno)<br />\n");
$out=postheader($url, $server, $cookies, $daten);
//echo $out;
fwrite($fp, $out);
while (!feof($fp))
{
$string.= fgets($fp, 128);
}
fclose($fp);
$daten should be a one dimensional associative array containing the data you want to send via the post request. For cookies, just add an empty array.
$server is the host address, and $url is only the address on the server, e.g.
$server="stackoverflow.com";
$url="/post.php";
$string will contain the whole response including the headers. If you don't want them, do a substring from the first "\r\n\r\n" occurence.
You can use Client side cookies:
<?php
setcookie("token", $myToken);
?>
The biggest fault with this method is that it absolutely requires the user to have cookies enabled on their browser. In highly secure situations a user may not have cookies enabled so this method wouldn't work for them.
Use $_SESSION global variable:
<?php
$_SESSION['token'] = $myToken;
?>
As previous variant, it requires cookies to be enabled.
Add $_GET parameter to URI string:
http://example.com?token=f4FArqk53Gwr4fESC73FedG48Trd3YEj
The biggest fault of this method is that your URI string will look very complex.
Also state information will be lost if user will manually modify URI.
Or pass everything in Hidden form fields:
Unfortunately it requires you to submit a form on every page.
You can do it with jQuery.
Some examples are here.
On further consideration, it looks like I was making things much more complicated than they needed to be. Thank you all for your help, but the solution seems to be that I should be a bit more intelligent.

GET method causes an error

sorry for the title but I'm not reaaly sure how to call this.
I am registered to a ssm service which enables sending automatic sms with a php script.
the script basically builds an xml string with all of the sms parameters (sendername, ..).
then it uses this to send it :
$sms_host = "api.inforu.co.il"; // Application server's URL;
$sms_port = 80; // Application server's PORT;
////.... generating query
$sms_path = "/SendMessageXml.ashx"; // Application server's PATH;
$fp = fsockopen($sms_host, $sms_port, $errno, $errstr, 30); // Opens a socket to the Application server
if (!$fp){ // Verifies that the socket has been opened and sending the message;
echo "$errstr ($errno)<br />\n";
echo "no error";
} else {
$out = "GET $sms_path?$query HTTP/1.1\r\n";
$out .= "Host: $sms_host\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)){
echo fgets($fp, 128);
}
fclose($fp);
the query is fine if I paste this
$url = "http://api.inforu.co.il/SendMessageXml.ashx?" . $query;
directly in the browser, then the sms gets send.
so the problem is that I'm getting an error
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /SendMessageXml.ashx
You have to urlencode() your $query, if you paste it to browser, the browser will encode it for you, but when you are dealing with a socket, you have to do it yourself.

How do you get the HTTP status code for a remote domain in php?

I would like to create a batch script, to go through 20,000 links in a DB, and weed out all the 404s and such. How would I get the HTTP status code for a remote url?
Preferably not using curl, since I dont have it installed.
CURL would be perfect but since you don't have it, you'll have to get down and dirty with sockets. The technique is:
Open a socket to the server.
Send an HTTP HEAD request.
Parse the response.
Here is a quick example:
<?php
$url = parse_url('http://www.example.com/index.html');
$host = $url['host'];
$port = $url['port'];
$path = $url['path'];
$query = $url['query'];
if(!$port)
$port = 80;
$request = "HEAD $path?$query HTTP/1.1\r\n"
."Host: $host\r\n"
."Connection: close\r\n"
."\r\n";
$address = gethostbyname($host);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);
socket_write($socket, $request, strlen($request));
$response = split(' ', socket_read($socket, 1024));
print "<p>Response: ". $response[1] ."</p>\r\n";
socket_close($socket);
?>
UPDATE: I've added a few lines to parse the URL
If im not mistaken none of the php built-in functions return the http status of a remote url, so the best option would be to use sockets to open a connection to the server, send a request and parse the response status:
pseudo code:
parse url => $host, $port, $path
$http_request = "GET $path HTTP/1.0\nHhost: $host\n\n";
$fp = fsockopen($host, $port, $errno, $errstr, $timeout), check for any errors
fwrite($fp, $request)
while (!feof($fp)) {
$headers .= fgets($fp, 4096);
$status = <parse $headers >
if (<status read>)
break;
}
fclose($fp)
Another option is to use an already build http client class in php that can return the headers without fetching the full page content, there should be a few open source classes available on the net...
This page looks like it has a pretty good setup to download a page using either curl or fsockopen, and can get the HTTP headers using either method (which is what you want, really).
After using that method, you'd want to check $output['info']['http_code'] to get the data you want.
Hope that helps.
You can use PEAR's HTTP::head function.
http://pear.php.net/manual/en/package.http.http.head.php

Categories