I got problem which is session out. The problem is I can connect to website with login panel, but after all when I click other links onto website , it was like as if i did not log in the website. So, how can i code something to keep online to website? What should i do for this?
<?php
function login ($url,$information){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $information);
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt");
curl_exec($curl);
curl_close($curl);
}
login("http://www.na.edu/online/login/index.php","username=user&password=blablabla");
?>
Related
I am trying to access a page, say http://www.domain.com/profile. The action and login urls are the same, and I am trying to save cookies to http://www.example.com/cookies.txt for authentication. Here is the code I'm using:
$loginURL = 'http://www.domain.com/login';
$COOKIE_FILE = 'http://wwww.example.com/cookies.txt';
$postValues = array(
'username' => 'myusername',
'password' => 'mypassword'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $loginURL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, $COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Chrome/35.0.2309.372');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, $loginURL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_exec($curl);
if(curl_errno($curl)){throw new Exception(curl_error($curl));}
// now we are logged in, attempt to access a password-protected page
curl_setopt($curl, CURLOPT_URL, 'http://www.domain.com/profile');
curl_setopt($curl, CURLOPT_COOKIEJAR, $COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($curl);
It seems like the curl requests are successful, but I am getting echoed a page from the server saying "Unauthorized Access". I don't think my cookies system is working correctly? How can I check that? How do I fix it?
Set CURLOPT_COOKIEFILE to same path as CURLOPT_COOKIEJAR. curl reads from file and writes to jar.
ETA: One likely reason your script doesn't work is that you don't send the cookie data from the first request in the second request.
So the actual answer was that the login was through CAS, which necessitates a much more complicated authentication process. However, my code above was improperly using CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR, so after I made those fixes I discovered I needed a rework of the approach.
I need some help, I have a ftp access and my problem is I want to display or get the content of a specific file since I will use as a variable in php. How is it possible? I have this code but it just show the content of the directory.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp://$HOST");
curl_setopt($curl, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'CWD /$PATH');
curl_exec($curl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'MLSD');
$ftp_result=curl_exec($curl);
echo $ftp_result;
Try
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp://$HOST/$PATH");
curl_setopt($curl, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$ftp_result=curl_exec($curl);
echo $ftp_result;
I have a simple PHP script that make a login to desired webpage. It is working OK on my localhost (wamp server), but when I run it on Heroku I get response like this (link: image link:)
Script is simple, just like that:
<?php
$username = "username";
$password = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec($ch);
echo $content;
?>
I don't know what am I doing wrong. Is it something wrong with Heroku or is just luck that code is working OK on my wamp server? I also tried running this script on other free hosting pages but they don't support CURLOPT_FOLLOWLOCATION parameter and so script is not working.
The text you're seeing at the top of the page is the header that you requested with :
curl_setopt ($ch, CURLOPT_HEADER, true);
Also, CURLOPT_POST is implicit when you use CURLOPT_POSTFIELDS, not need for it.
You're code should look something like:
<?php
$username = "username";
$password = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec($ch);
echo $content;
Looks like that page that I was trying to post to has country restricted IP.
I am so confused about my cURL script requesting the target page "facebook.com" for example, with the same port as the proxy.
example:
proxy is: 178.215.111.70:9999 . a cURL error says:
Failed connect to facebook.com:9999; Connection timed out
I see that it tries to connect to facebook using the poxy's port: 9999
Here is my code:
<?php
$curl = curl_init("https://facebook.com");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_REFERER, 'https://www.facebook.com');
if ($var) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "test");
}
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,9999999);
curl_setopt($curl, CURLOPT_TIMEOUT, 0);
curl_setopt($curl, CURLOPT_PROXY, '178.215.111.70');
curl_setopt($curl, CURLOPT_PROXYPORT, '9999');
$result['exe'] = curl_exec($curl);
$result['err'] = curl_error($curl);
curl_close($curl);
echo $result['err'];
echo $result['exe'];
?>
I solved my problem.
It was a problem in the server only. When i tried the SAME script in another server it worked 100% fine
So always make sure of the configuration in your server when you face a problem like this.
I'm writing a function to fetch the captcha, of course it needs to keep the session. I'm using cookie file for curl and use it for every request but it does not work. When I view the cookie file, I see that the PHPSESSID changed each time I call the function. How could I solve it?
Here is my code
<?php
function fetch_captcha($url, $cookie_file = false, $user_agent = DEFAULT_USER_AGENT, $timeout = 10) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if ($cookie_file) {
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
}
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Don't use:
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
PHPSESSID is a session cookie, and this option causes each cURL call to start a new session, so it ignores all session cookies in the file.