CURL with Cookie problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

CURL with Cookie problem

Post by jwalsh »

Hi,

Trying to do a simple login, where the external site sets a session cookie. I think maybe I have the cookie file path wrong.

I'm on a shared host, linux, do I need to put the full path to the cookie file?

I get a login error page each time,

Thanks,

Josh

Code: Select all

$url = "http://www.example.com/checklogin.php";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url); 				// SET URL
		curl_setopt($ch, CURLOPT_REFERRER, "http://example.com/login.php");
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);			// FAIL ON ERROR
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 		// ALLOW REDIRECTS
		curl_setopt($ch, CURLOPT_COOKIEFILE, "/home/selltra/public_html/admin/cook");	// COOKIES
		curl_setopt($ch, CURLOPT_COOKIEJAR, "/home/selltra/public_html/admin/cook");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		// RETURN TO VARIABLE
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);				// TIMES OUT AFTER 4 SEC
		curl_setopt($ch, CURLOPT_POST, 1);					// SET POST METHOD
		curl_setopt($ch, CURLOPT_POSTFIELDS, "username={$config['username']}&password={$config['password']}&Submit2=Submit"); 	// POST VALUES
		$result = curl_exec($ch);
		
		if (! $result) {
			echo "Server Connection Problem";
		} else {
			echo $result;
		}
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I had a problem with cookies and curl a long time ago .. I *think* the problem was needing permission to create a file in the particular directory it was trying to save the cookie to. Make sure Apache/PHP has the correct permissions (chmod 755 usually does the trick).
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Tried that, no avail. Both the directory and file are already 755.

Any other suggestions? Are sessions treated different than cookies through cURL?

Josh
Post Reply