$ch = curl_init();
	$POST = 'email='.urlencode('username').'&password='.urlencode('password');
	curl_setopt ($ch, CURLOPT_URL, "http://login.myspace.com/index.cfm?fuseaction=login.process");
	curl_setopt ($ch, CURLOPT_HEADER, 0);
	curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt ($ch, CURLOPT_UNRESTRICTED_AUTH, true);
	curl_setopt ($ch, CURLOPT_POST, true);
	curl_setopt ($ch, CURLOPT_POSTFIELDS,$POST);
	curl_setopt ($ch, CURLOPT_COOKIEJAR, "/cookie.txt");
	$contents = curl_exec($ch);
	curl_close($ch);

echo $contents;

"echo $contents" should return my "home page" at myspace. however, it returns a myspace webpage asking me to login. my credentials (cookies) are not being carried all the way through to my home page. the myspace login requires at least a couple "follows" or "redirects."

But this code works fine on my Windows server running XAMPP. 😕 But when I moved the application to my linux box, the cookies won't "followlocation." I can use the curl library just fine under simpler situations (like just a simple webpage fetch), so I don't think there is a problem with the installation or proxies or anything.

I think there is a problem with the way I'm trying to use the cookies. Do I have to specify a certain directory? Do I need to spell it out all the way from the root? Does the cookie.txt file actually need to exist? (FYI, no physical cookie file was needed for operation on my Windows server) I've tried adding a COOKIEFILE to this one, but no success.

My server is a VPS account, so I have full control over settings and configuration. PHP safe mode is off, open_basedir = null.

thanks for your help fellas! 🙂

    I have since added a real cookie file to the server, and updated the code to ensure that I have read/write access to it.

    still doesn't work though :rolleyes:

    $postfields = 'email='.urlencode('username').'&password='.urlencode('password');
    	$url = "http://login.myspace.com/index.cfm?fuseaction=login.process";
    	$cookie_file_path = "cookies/cookiejar.txt"; 
    	$fp = fopen("$cookie_file_path","r+") or die("<BR><B>Unable to open cookie file $mycookiefile for write!<BR>"); 
    	fclose($fp); 
    
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
    $contents = curl_exec ($ch); 
    curl_close ($ch); 
    
    echo $contents;

      I created a MySpace login using php a while back, I am not positive but I think the problem may be that you do not send the token with the login.
      When you go to their front page it sets some info and if you view the source the login form is like this for me:
      <form action="http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken=ab5c393a-1043-4859-958c-b89ff3198628"

      You may need to load the front page and extract that value. I have some code if you'd like to see. It uses a basic curl wrapper I made so I can include that if you want as well.

        Drew, that'd be great. please send whatever you have.

        as far as that token goes...

        i've never addressed it before on my windows server, and everything works great! i have no clue why it won't work on the unix box...

          what is also weird about these cookies on the windows box is that i don't ever see a cookie file ever being created.... i've checked the entire machine but no "cookie.txt" exists. and i use the application on there all the time... I wish i could just server this website off my windows laptop!

          anyone know a good windows vps? :rolleyes:

            oh yea, you make a good point that it is okay on the windows server without it.

            anyway, here is the curl code

            <?php
            
            class CurlHTTPRequest
            {
              var $cookieFile;
              var $headers = array();
              var $UserAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1';
              var $useGZIP = FALSE;
            
              var $lastUrl;
            
              var $httpHeader;
              var $httpBody;
            
            
              var $ch;
            
              function CurlHTTPRequest($cookieFile)
              {
                $this->ch = curl_init();
                $this->cookieFile = $cookieFile;
              }
            
            
              function PostData($url, $postdata, $referer = '', $followlocation = TRUE)
              {
                curl_setopt($this->ch, CURLOPT_URL, $url);
                curl_setopt($this->ch, CURLOPT_USERAGENT, $this->UserAgent);
                curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookieFile);
                curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookieFile);
                curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
                curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, $followlocation);
                curl_setopt($this->ch, CURLOPT_HEADER, 1);
                curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
                if ($referer != '')
                  curl_setopt($this->ch, CURLOPT_REFERER, $referer);
            
            if ($this->useGZIP)
              curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip, deflate');
            else
              curl_setopt($this->ch, CURLOPT_ENCODING, 'none');
            
            curl_setopt($this->ch, CURLOPT_POST, 1);
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postdata);    
            
            $this->lastUrl = $url;
            
            $resp = curl_exec($this->ch);
            
            list($this->httpHeader, $this->httpBody) = explode("\r\n\r\n", $resp, 2);    
              }
            
              function GetData($url, $referer = '', $followlocation = TRUE)
              {
                curl_setopt($this->ch, CURLOPT_URL, $url);
                curl_setopt($this->ch, CURLOPT_USERAGENT, $this->UserAgent);
                curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookieFile);
                curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookieFile);
                curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
                curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, $followlocation);
                curl_setopt($this->ch, CURLOPT_HEADER, 1);
                curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
                if ($referer != '')
                  curl_setopt($this->ch, CURLOPT_REFERER, $referer);
            
            if ($this->useGZIP)
              curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip, deflate');
            else
              curl_setopt($this->ch, CURLOPT_ENCODING, 'none');
            
            $this->lastUrl = $url;      
            
            $resp = curl_exec($this->ch);
            
            list($this->httpHeader, $this->httpBody) = explode("\r\n\r\n", $resp, 2);      
              }
            
              function SetHeader($key, $value)
              {
                $this->headers[] = "$key: $value";
              }
            
              function SetUserAgent($useragent)
              {
                $this->UserAgent = $useragent;
              }
            
              function SetProxy($proxy)
              {
                curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
              }
            
              function RemoveProxy()
              {
                curl_setopt($this->ch, CURLOPT_PROXY, '');
              }
            
            }
            
            ?>
            

            and the login code...

            <?php
            
            define('USERNAME', 'you@youremail.com');
            define('PASSWORD', 'password');
            
            
            require_once 'curl.php';
            
            $curl = new CurlHTTPRequest(USERNAME . '.txt');  // make sure the server can write to the current directory.
            
            $curl->GetData('http://www.myspace.com/');
            
            preg_match('/MyToken=([^"]+)"/', $curl->httpBody, $token);
            
            $curl->PostData("http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken={$token[1]}", 'email=' . USERNAME . '&password=' . PASSWORD . '&loginbutton.x=5&loginbutton.y=5');
            
            //echo $curl->httpBody;
            
            if (preg_match('@Cool New People</h5>\s+<div[^>]+>\s+<a href="([^"]+)"><span>([^<]+)</span>@is', $curl->httpBody, $furl)) {
              echo "Logged in\n";
            } else {
              echo "Didn't log in";
            ?>
            

            this has worked for me in the past, i haven't tried it out for a few months, but it was okay then. hopefully it can help you out.

              Actually, I just tested it and it logged me in fine, so it should work out for you if there are no problems with file permissions or anything on the linux machine.

              edit: it appears with the code you have for windows, the cookie should be created in C:/cookie.txt or whatever your drive is. since its got the /cookie.txt it'll go all the way to the root level of the drive.

              this app may be useful for seeing if it is created and where. http://www.microsoft.com/technet/sysinternals/utilities/filemon.mspx

                drew,

                I uploaded your code, but no luck. i even changed the www directory to full access (chmod 777). no luck. Your code worked fine on the windows laptop though. 🙂

                if you think you can get this thing to work, i'll give you access to my server and $50. seriously. paypal, us mail check, whatever you want. 🙂

                  Well folks, just wanted to post the solution to my cookie problem.

                  The reason my code worked on windows and not unix is the difference in file permission granting.

                  On the unix box, I gave full access to the world on my root www directory, but those permissions did not propogate through to subfiles and subdirectories like I expected it would.

                  so after giving appropriate read/write access to my cookie file and directory on the unix box, it all worked fine.

                  on windows, i've discovered you can do just about anything you want and it will still work. you don't even need a real cookie file!

                  final code that works just fine after granting "757" the "cookie" directory and cookie file:

                  [code=php]$ch = curl_init();
                  $POST = 'email='.urlencode('username').'&password='.urlencode('password');
                  curl_setopt ($ch, CURLOPT_URL, "http://login.myspace.com/index.cfm?fuseaction=login.process");
                  curl_setopt ($ch, CURLOPT_HEADER, false);
                  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
                  curl_setopt ($ch, CURLOPT_FRESH_CONNECT, true);
                  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
                  curl_setopt ($ch, CURLOPT_UNRESTRICTED_AUTH, true);
                  curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
                  curl_setopt ($ch, CURLOPT_POST, true);
                  curl_setopt ($ch, CURLOPT_POSTFIELDS,$POST);
                  curl_setopt ($ch, CURLOPT_REFERER, "http:/myspace.com/");
                  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0");
                  curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies/cookie.txt");
                  $contents = curl_exec($ch);
                  curl_close ($ch);
                  unset($ch);
                  
                  echo $contents;  // this should output user home page[/code]
                    Write a Reply...