Working with session cookies and PHP

Translation of the original in Catalan. Read it in: CatalàCastellano

Sometimes we need to talk to a web site from PHP and it uses session cookies without which we can't talk to the web site. To grab the login cookie we can use this function:

function getgaleta($server, $port)
{
   $cont = "";
   $ip = gethostbyname($server);
   $fp = fsockopen($ip, $port);
   if (!$fp)
   {
       return "Unknown";
   }
   else
   {
       $com = "GET http://www.barcelonastyle.com/gc/web/webesp/framesetxsl.asp HTTP/1.0rnrn";
       fputs($fp, $com);
       while (!feof($fp))
       {
           $cont .= fread($fp, 500);
       }
       fclose($fp);
       return substr($cont, strpos($cont, "Set-Cookie: ")+12, 45);
   }
}

If now that we already have the cookie what we want to do is use it to make requests against the site, we can use this other function:

function getcontent($server, $port, $file,$galeta)
{
   $cont = "";
   $ip = gethostbyname($server);
   $fp = fsockopen($ip, $port);
   if (!$fp)
   {
       return "Unknown";
   }
   else
   {
       $com = "GET http://".$server.$file." HTTP/1.0rnCookie:".$galeta."rnrn";
       fputs($fp, $com);
       while (!feof($fp))
       {
           $cont .= fread($fp, 500);
       }
       fclose($fp);
       return $cont;
   }
}

Silly but useful, I have many many more functions but there's no point in overwhelming you, only that I found these ones useful along with the two previous ones.