Jump to content

Archived

This topic is now archived and is closed to further replies.

snapper64

PHP Login to site help

Recommended Posts

Hi Guys,

I need to create a script that logs me in to http://tracks.tra.in/login. So i visit www.mydomain.com/tracks.php, it logs me in using the data stored in the php file and then sends me to http://tracks.tra.in/.

I have got this so far:

[code]<?php
$cookieJar = \\\\\\\"cookie\\\\\\\";
//this should be set to whatever file on the computer you want to save and retrieve cookies to/from

$userAgent = \\\\\\\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5\\\\\\\";

$postData = \\\\\\\"user_login=gwbush&user_password=password\\\\\\\";

$url_login = \\\\\\\"http://tracks.tra.in/login\\\\\\\";
$url_view  = \\\\\\\'http://tracks.tra.in/\\\\\\\';

$ch = curl_init(); //creates and initializes a curl session
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar); //sets the file to save cookies in after recieving them from the page
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar); //sets file to take cookies from to give to the site if needed
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); //sets the user agent string to make it look like this is a real browser
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //if a simple http redirect is recieved, setting this to 1 will cause curl to follow the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //this causes the data retrieved to be returned by the curl_exec function below instead of being printed
curl_setopt($ch, CURLOPT_URL, $url_login); //sets the url to request
curl_setopt($ch, CURLOPT_POST, 1); //this is a post request and not a get request like normal
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); //the post data to be sent with the request
$data = curl_exec ($ch); //execute the request
curl_close ($ch); //close the curl session
unset($ch); //destroy the curl session.

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt($ch, CURLOPT_URL, $url_view);
$result = curl_exec ($ch);
curl_close ($ch);
echo $result;

?>[/code]

My problem is when i execute this script it logs me in but shows the http://tracks.tra.in/ home page on my domain (www.mysite.com/tracks.php shows http://tracks.tra.in/). Once the script has logged me in i need to be redirected to that site but still stay logged in.

Please help . . .

Thanks,

Charlie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.