Jump to content

How to file_get_contents when login required?


MasterK

Recommended Posts

Hello!

 

I am trying to read data from a page that you have to be logged into to view, I am trying to use file_get_contents to do this, I have read that I need to use cURL, Is that true?

 

Also, as Another Option, Well I'm not sure how to explain what I want to do but let me try

 

I want to go to the remote website, Login, and then run my script as I'm logged in.. If that make sense

 

Basically, Right now I go to the website, Login, and then use file_get_contents to pull the website and I'm not logged in, Is there anyway that I can make it read that I am logged in? :confused: :confused: :confused:

 

Link to comment
Share on other sites

No, you don't need to use cURL. You can, but it is not required. Exactly what you need to do depends on the auth systems in place on the remote website; often you'll need to log in, saving the cookie that is returned on successful login, then access the protected page sending over that cookie.

 

If you could give some more details of the procedure you would normally go through to browse to the page in question, we would be able to give you help with the finer details of what to do.

Link to comment
Share on other sites

To get to the page, I just login and then Click on a link...

 

Here is the Login code from the website I am trying to access

 

<script type="text/javascript" language="javascript">
			var tblLogin = document.getElementById("tdLogin");
			if (tblLogin) {
				var strForm = '<table border="0">';
				strForm = strForm + '<tr><td>Username:</td><td><input name="U"></td></tr>';
				strForm = strForm + '<tr><td>Password:</td><td><input name="P" type="Password"></td></tr>';
				strForm = strForm + '<tr><td colspan="2" align="center"><input name="sublogin" id="sublogin" type="submit" value="Login"></td></tr>';
				strForm = strForm + '<tr><td colspan="2" align="center"><a href="lostpw.php">Reset My Password</a></td></tr>';
				strForm = strForm + '</table>';

				tblLogin.innerHTML = strForm;
				//<tr><td colspan="2" align="center"><a href="servers.php">Change Servers</a></td></tr>
			}
			/*
			var tdReset = document.getElementById("tdReset_PW");
			if (tdReset) {
				tdReset.innerHTML = '<a href="lostpw.php">Reset My Password</a>';
			}
			*/
		</script>

Link to comment
Share on other sites

I've been playing around with every thing you guys have said, and I'm just not having any luck...

 

My Code

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "website");

curl_setopt ($ch, CURLOPT_POST, 1);

curl_setopt ($ch, CURLOPT_POSTFIELDS, "U=username&P=password");

curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$login = curl_exec ($ch);

curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookie.txt");

curl_setopt($ch, CURLOPT_URL, "membership page");

$members = curl_exec ($ch);

curl_close ($ch);
print($members);
?>

 

:confused: It doesn't work, It's not logged when it shows the Membership page

Link to comment
Share on other sites

Make sure the cookie file is being created - and I believe you need to "restart" cURL (initiate it again) after you execute it.

 

Cookie is being created

 

I tried this

 

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "website");

curl_setopt ($ch, CURLOPT_POST, 1);

curl_setopt ($ch, CURLOPT_POSTFIELDS, "U=username&P=password");

curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$login = curl_exec ($ch);

$ch = curl_init();

curl_setopt ($ch, CURLOPT_COOKIEFILE, "cookie.txt");

curl_setopt($ch, CURLOPT_URL, "membership page");

$members = curl_exec ($ch);

curl_close ($ch);
print($members);
?>

 

Is that what you meant?  :confused: I am really an Idiot when it comes to cURL  :-[

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.