fonecave Posted November 5, 2007 Share Posted November 5, 2007 hi guys im trying to create a script in my php page that validates a login from a text file the text file reads like this username password username password and so on all different i want the php page to take the user and password fields and check them against the pairs in the txt file ive got soo far but i cant seem to get any further if the login is valid i want to set a cookie called user with the value of the username for an hour here is the code so far can anyone help <?php $check = "0"; if (isset ($_POST['login'])) { $name=trim($_POST['name']); $password=trim($_POST['password']); if (empty($name) || empty($password)) { echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="user" /><br>'; echo '<br>'; echo '<input type="password" name="password" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; echo "<font color='red'>Please Enter Username and Password!</font>"; } else { echo "hi"; $file=fopen("userlogincheck21071985.txt","r") ; while ( !feof($file) && $check == "0") { $f_user = trim(fgets($file)); $f_password = trim(fgets($file)); if($f_user == $user && $f_password == $password) { echo 'login succesful'; } else { echo 'login failed'; } } } } else { echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="user" /><br>'; echo '<br>'; echo '<input type="password" name="password" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/ Share on other sites More sharing options...
revraz Posted November 5, 2007 Share Posted November 5, 2007 So did you need help in setting the cookie? Also, you should consider using CSS to get rid of all that formating in your code. Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385179 Share on other sites More sharing options...
fonecave Posted November 6, 2007 Author Share Posted November 6, 2007 basically i want the code to check the text file to see if the posted username and passwords match like i have tried to do and if they do set a cookie as i said earlier i am just at a dead end i cant get any of the code to work.. any chance anyone could sort it out for me? Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385354 Share on other sites More sharing options...
kratsg Posted November 6, 2007 Share Posted November 6, 2007 Set this code up as the would do for an .htpasswd file (as those are most efficient in any scenario). username:password or username|password Then, simply use $line = fgets($file) to go line by line list($username,$password) = explode('[DELIMITER]',$line).. And if $username = $log-in info, then check passwords, etc... Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385372 Share on other sites More sharing options...
fonecave Posted November 6, 2007 Author Share Posted November 6, 2007 im new to this and that means nothing to me!! i want to open the file and do something like $user = fgets($file) $pass = fgets($file) if ($user == $_POST['user'] && $pass == $_POST['password']) set a cookie but i need to loop through the text file so that it checks every 2 lines Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385618 Share on other sites More sharing options...
kratsg Posted November 6, 2007 Share Posted November 6, 2007 Get the file formatted as such: user/pass file user1:pass1 user2:pass2 The php file: function check_user($check_user,$check_pass){ $filename = "some_file.txt"; $file = fopen($filename,'r'); while($line = fgets($file)){//loop through line by line list($username,$password) = explode(":",$line);//break up each line into the username and password if($username == $check_user){//we found the corresponding user, let's check the password if($password == $check_pass){//it matches setcookie("logged_in",true,time()+3600); return true; } else { echo "I'm sorry, the username/password combination did not match."; return false; } } } return false;//always return false by default if we reach here somehow } Just run the function like so: check_user("bob","mypassword"); Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385639 Share on other sites More sharing options...
fonecave Posted November 6, 2007 Author Share Posted November 6, 2007 i have this code that seems to work fine if login is correct it prints line "login Successful" otherwise "invalid login" in both fields arent filled in "Please enter username and password!" However where i have the successful login i want to set a cookie but it wont let me me because the php script isnt in the head of the page! what do i do, if i move it up into the head will it still work properly or can i set the cookie in the head if the login was successful below? <?php if (isset ($_POST['login'])) { $username=trim($_POST['username']); $pass=trim($_POST['pass']); if (empty($username) || empty($pass)) { echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="username" /><br>'; echo '<br>'; echo '<input type="password" name="pass" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; echo "<font color='red'>Please Enter Username and Password!</font>"; } else { $passok = "0"; $file=fopen("userlogincheck21071985.txt","r") ; while ( !feof($file)) { $f_user = trim(fgets($file)); $f_password = trim(fgets($file)); if ($f_user == $username && $f_password == $pass) { $passok = "1"; } } echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="username" /><br>'; echo '<br>'; echo '<input type="password" name="pass" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; if ($passok =="0") { echo "<FONT color='red'>Ivalid Login</font>"; } if ($passok =="1") { echo "<FONT color='red'>Login Successful</font>"; } } } else { echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="username" /><br>'; echo '<br>'; echo '<input type="password" name="pass" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385652 Share on other sites More sharing options...
fonecave Posted November 6, 2007 Author Share Posted November 6, 2007 i mean its not above the HTML tag Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-385657 Share on other sites More sharing options...
kratsg Posted November 7, 2007 Share Posted November 7, 2007 Try the following dirty fix, although you may have to re-fix your logic a little bit (IE: echo everything AFTER you do all your php work). At the top of the file, right below <?php add ob_start(); Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-386274 Share on other sites More sharing options...
MadTechie Posted November 7, 2007 Share Posted November 7, 2007 Set the cookie before output so move the script to the start of the file and see code below comment <?php if (isset ($_POST['login'])) { $username=trim($_POST['username']); $pass=trim($_POST['pass']); if (empty($username) || empty($pass)) { echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="username" /><br>'; echo '<br>'; echo '<input type="password" name="pass" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; echo "<font color='red'>Please Enter Username and Password!</font>"; } else { $passok = "0"; $file=fopen("userlogincheck21071985.txt","r") ; while ( !feof($file)) { $f_user = trim(fgets($file)); $f_password = trim(fgets($file)); if ($f_user == $username && $f_password == $pass) { $passok = "1"; //ADD COOKIE HERE setcookie("login", $username, time()+3600); //whatever! } } echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="username" /><br>'; echo '<br>'; echo '<input type="password" name="pass" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; if ($passok =="0") { echo "<FONT color='red'>Ivalid Login</font>"; } if ($passok =="1") { echo "<FONT color='red'>Login Successful</font>"; } } } else { echo '<form action="login.php" method="post"'; echo 'enctype="multipart/form-data">'; echo '<input type="text" name="username" /><br>'; echo '<br>'; echo '<input type="password" name="pass" />'; echo '<br>'; echo ' '; echo ' '; echo ' <input type="submit" '; echo 'name="login" value="Login" />'; echo '</form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/76094-php-login/#findComment-386315 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.