[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 977
  • Last Modified:

PHP url open and get contents


I have a small application to make, in which using PHP you open a URL, authenticate your self, and then get the result.

The way it works is: POST data to the url 'http://www.somesite.com/login.php'
the POST data is

'username' = 'user'
'password' = 'pass'
'returnto' = 'content.php?id=123'

What I have accomplished is, sending the POST data, but the result I get is

HTTP/1.1 302 Found ,Date: Fri, 22 Apr 2005 17:18:40 GMT ,Server: Apache/1.3.33 (Unix) PHP/4.3.10 ,X-Powered-By: PHP/4.3.10 ,Set-Cookie: uid=179375; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/ ,Set-Cookie: pass=0eb8a10dabe379785b43a64add2039cd; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/ ,Location: http://www.somesite.com/content.php?id=123 ,Connection: close ,Transfer-Encoding: chunked ,Content-Type: text/html , ,0 , ,

Which I understand is the script is telling the broswer to redirect to that page. HOWEVER what I am concerned about is, how to send the cookies to the redirected page ?

I am POSTing data in this way:

$postdata is array [
  'username' => 'user'
  'password' => 'pass'
  'returnto' => 'content.php?id=123'
foreach($data as $i => $v) {
  $postdata .= $i . "=" . urlencode($v) . "&";

$fp = @fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30);
fputs($fp, "POST $web[path] HTTP/1.1\r\n");
fputs($fp, "Host: $web[host]\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($postdata). "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $postdata . "\r\n\r\n");

while (!feof($fp)) {
  $info[] = @fgets($fp, 1024);
$info = implode(",", $info);

Then $info is what I am returned by the server.

So my question really is on how to pass cookies now to the content.php file, because if I dont send the cookies, then I am redirected to the login page.

1 Solution
you could read in the data that is returned until you find Set-Cookie:  (i would suggest using explode() on the returned value to split on the lines and you could use the string position strpos() of Set-Cookie: to find the line).  Then you could append this cookie onto your new post string.
You should be able to use this for finding the redirect location as well (which is better than hard coding it in case it changes in the future)

alternatively you could use preg_match() and write a fairly simple regex string to locate the value.  it should be something like (i'm bad at regex, this is probably wrong, i apologise, but i hope it helps you get an idea):
$regex="/Set-Cookie: ([a-zA-Z0-9]+)\r\n/";

index 1 stores the value of the cookie then.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now