MasterK Posted October 17, 2009 Share Posted October 17, 2009 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: Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/ Share on other sites More sharing options...
Kaboom Posted October 17, 2009 Share Posted October 17, 2009 heres what i do: if (!isset ($_SESSION ['user'] [0])) else header 'Location: login.php' Not exactly thats just a guess but mines close and it works so no cURL Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938518 Share on other sites More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 What type of login is it on the page you are requesting? Is it protected by the server or is it a scripted login? As in is it a PHP login? Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938519 Share on other sites More sharing options...
Kaboom Posted October 17, 2009 Share Posted October 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938521 Share on other sites More sharing options...
Coreye Posted October 17, 2009 Share Posted October 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938527 Share on other sites More sharing options...
Kaboom Posted October 17, 2009 Share Posted October 17, 2009 so.. like they visit a page but you need to login to view and when you do it takes you back there like when making a post? Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938534 Share on other sites More sharing options...
salathe Posted October 17, 2009 Share Posted October 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938578 Share on other sites More sharing options...
MasterK Posted October 17, 2009 Author Share Posted October 17, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938728 Share on other sites More sharing options...
waynew Posted October 17, 2009 Share Posted October 17, 2009 Have a look at the examples below: http://www.trap17.com/index.php/automatic-login-curl_t38162.html http://www.weberdev.com/get_example-4277.html http://ryan.ifupdown.com/2009/06/30/login-with-curl-and-php/[/code] Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-938732 Share on other sites More sharing options...
MasterK Posted October 20, 2009 Author Share Posted October 20, 2009 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); ?> It doesn't work, It's not logged when it shows the Membership page Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-940162 Share on other sites More sharing options...
Stephen Posted October 20, 2009 Share Posted October 20, 2009 Make sure the cookie file is being created - and I believe you need to "restart" cURL (initiate it again) after you execute it. Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-940164 Share on other sites More sharing options...
MasterK Posted October 20, 2009 Author Share Posted October 20, 2009 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? I am really an Idiot when it comes to cURL Quote Link to comment https://forums.phpfreaks.com/topic/178003-how-to-file_get_contents-when-login-required/#findComment-940679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.