I posted this question earlier but it was misinterpreted by those reading it and was closed before I had time to clarify. If you don't understand what I mean, please ask!
I have a site, let's call it "site A". On "site A", there is a log in page. On this page, you POST a username and password to a PHP script. If the login details are correct, the PHP script sets a cookie on the browser. This cookie is called "SESSION".
When you view the site, it checks whether "SESSION" is valid, and displays either the information or the login page.
I want to connect to the page via PHP and POST the login details. I then want to store the "SESSION" cookie via PHP, and display the contents of the page (again, via PHP).
How would I do this?
You can use PHP as a web client as well. You can use the cUrl library to make requests from PHP.
You can use setopt to set all kinds op options for your cUrl session, including POST (CURLOPT_POST) and the POST variables (CURLOPT_POSTFIELDS), but also choose a kind of authentication (CURLOPT_HTTPAUTH) in case the site doesn't use normal post for this.
I found an example that might be useful here: http://davidwalsh.name/curl-post, although you can find many other examples by Googling for something like 'php curl post'.
Related
I'm attempting to automatically download content at regular time intervals from a site requiring users to log in. The content I'm seeking to download is a small .js file (<10 kb).
As the site will display the desired data only when I'm logged in, I'm unable to simply use functions such as urlwrite (in MATLAB) to download the data.
I'm not sure whether the libcurl library in PHP would be able to solve the problem easily.
As suggested in the answer to this similar question (Fetching data from a site requiring POST data?), I've tried to use the Zend_Http_Client, but haven't been able to get it to work.
In summary, I'd like help on automatically downloading URL content from a site requiring user log-in (and presumably submission of cookies).
In addition to this, I'd appreciate advice on which software is best for automated download of such data at regular time intervals.
(If you do require the exact URL I am trying to download from to test a solution, please leave a comment below.)
It depends on the type of login the site uses. If it uses HTTP authentication you use curl option CURLOPT_HTTPAUTH (see setopt, http://php.net/manual/en/function.curl-setopt.php) Otherwise, as said, you use COOKIEJAR and possible COOKIEFILE.
Another option is the standalone utility wget. The FAQ contains a nice explanation of both login methods http://wget.addictivecode.org/FrequentlyAskedQuestions#password-protected
If this is the first time you use curl: don't forget to set CURL_RETURNTRANSFER to true (if false the content is send to stdout) and CURL_HEADER to false to get the content without headers.
I your only concern is login, rather than cookies in general. Check the answer to this question : How do I use libcurl to login to a secure website and get at the html behind the login
I know how to load a page from another website and analyze it but the website I'm trying to load some pages from, doesn't let unregistered users to visit those pages. I do have a username and a password to load those pages normally in my browser, but I'm wondering if I could do it in PHP or not? :/
I'm not sure what information I should give you about the website but if what I already told is incomplete just ask what information I should give.
Thanks.
Most websites use cookies to store information related to your authentication status.
To get past this programatically, you'll have to send this information everytime you make a request. Here is how you can get it -
Using a tool like firebug, inspect the cookies the site sends when you login manually.
Write code to capture this information and send this cookie with subsequent requests
Note: Do check the ToS of the site you are trying to scrape. Some sites do not permit you to scrape or use their content without prior permission
Research php SOAP. It includes the tools to log in to another site, giving your php script access to the HTML which would normally be presented to the browser. BTW, this is called scraping
I would like to enable an "auto login" button for my users. By pressing the button the users will be logged in to a different site with the username and password that I have added inside the code.
My site uses php and this site is written on asp.
Is this possible?
Thanks in advance
You'd have to use CURL in PHP in order to send the POST data to your ASP script in the remote site.
Nevertheless, the ASP site might have some inner-validations which can lead to refuse your request, it's worth a try, though!
To set CURL to user POST, check out the setopt CURL function options, you have to set the CURLOPT_POST option to TRUE, but you might find that (depending on the ASP site), you need to activate/deactivate other options.
Cheers!
This depends on the website you're trying to login to.
In a website not using a key-based system for each visitor this can be achieved pretty easily.
First navigate to the page yourself that contains the login form and show it's page source.
Jot down every <input>-tag's name and value and determine which one is the username & password. Also note the form's action to see where the data is going towards.
Now you can use curl to send a user to the website, just inject the post data and apply your own username & password to it.
A. If you don't own the remote site:
Have you tried to post your authentication parameters directly to the ASP.net page? Then you could also try using CURL but either way, be rest assured that the site owner might lock you out anytime by implementing simple CSRF protection.
B. If you own the remote site:
You can share sessions with a unique key using a common database.
I have looked at some examples and they use Curl in php etc but I just want a simple command that passes my credentials to a site so it can login and give me access to download a file. Current problem is when I try to use get_contents I get stuck at login page because it needs a login before it can allow a download so isnt there a way to send my login info before get_contents in php? Thanks
for example we can assume the website is located at www.confluence.com
You're out of luck. file_get_contents() can only get content, hence the name. You can try to authenticate via the get syntax for standard authentication, like http://username:password#example.com.
If you have to post your credentials via HTTP POST, you'll have to use curl.
The problem is that when you log in the server send to your browser a cookies that your browser automatically stores.
With file_get_content() you can actually pass cookies ( the third context parameter of file_get_content() can handle this).
Have a look at stream context create.
By the way, you need to first send your login info to the login page (with curl), when you recive cookies back, pass them as option to file_get_content() and the trick is done.
If the server is using a login system different from cookies let us know, so we can help you
I'm trying to get the source of a file using PHP and the file_get_contents function.
The problem is that the website requires a login. I thought that this could be bypassed by logging into the site first and then use the file_get_contents function but this is not the case. The page that the function returns is the login page.
This is the code:
<?php
if (isset($_GET['getroster'])){
$file = stripslashes(file_get_contents('https://intranet.hlf.de/custom/cddTUB.asp?show=1'));
print ($file);
}
?>
As you can see when running this function, or when going to the url, it requests a username, password and company from a dropdown list.
Is it possible to pass these values to the page by using this or any other PHP function. Or maybe doing it in a totally different way?
The cookies in your browser, and the cookies can be sent with PHP are completely unrelated.
Using curl you can set the options CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE to specify a file in which cookies should be stored and loaded from.
Doing that you will first have to make a request that does the login (this will get you the cookie). Next you can do the request you wanted to do as a logged in user (because now curl has the cookies).
You have to send the correct cookies. If you can use curl, then the script on this page should help. You can also take a look at this SO question (it's slightly different from your case since it's reusing browser cookies to make the request).
Any change you could post an example of how to do that Daniel?
There wouldn't be an option of creating a login like form which sents the user information to the page to login automatically?
Read something about the stream_context_create function which could do a login by sending header information but I really wouldn't where to start. The examples posted about that only give username and password but I ofcourse have a third option to post to the authentication page!