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:

 

Heres exactly what i put:

 

<?php
include "whatever.php":
if (!isset($_SESSION["user"][0])) {header('login.php'); die();}

?>

 

so if there not logged in goto login.php

 

He's not asking how to force users to login. He's asking how to use the function file_get_contents when a login is required.

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.

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>

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

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  :-[

Archived

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

×
×
  • 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.