PHP url open and get contents

Posted on 2005-04-22
Last Modified: 2012-05-05

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 ''
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: ,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.

Question by:glottis
    1 Comment
    LVL 6

    Accepted 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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now