TheJoey Posted October 12, 2009 Share Posted October 12, 2009 <?php session_start(); $usernamematch = $_POST['username']; $passwordmatch = $_POST['password']; $fp = fopen("registrationinfo/info.txt","a"); $array = explode(':', $fp); $username = trim($array[0]); //user-name $password = trim($array[2]); //password if ($username == $usernamematch && $password == $passwordmatch) { $_SESSION['loginsuccessfull'] = true; header('location indexsuccessfull.php'); } else { header('location wronginfo.php'); } ?> but at the moment its doing nothing just a blank page. im not sure how to work with txt files this way. any help is much appricatied Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/ Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 You seem to assume that opening a file is the same as reading it. $array = explode(':', $fp); should be something like: $fileData = fread($fp,filesize("registrationinfo/info.txt")); $array = explode(':', $fileData); Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935311 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 Should i remove the $fp = $fp = fopen("registrationinfo/info.txt","a"); ?? well with the modified code i still didnt get anything. <?php session_start(); $usernamematch = $_POST['username']; $passwordmatch = $_POST['password']; $fp = fopen("registrationinfo/info.txt","a"); $fileData = fread($fp,filesize("registrationinfo/info.txt")); $array = explode(':', $fileData); $username = trim($array[0]); //user-name $password = trim($array[1]); //password if ($username == $usernamematch & $password == $passwordmatch) { $_SESSION['loginsuccessfull'] = true; header('location indexsuccessfull.php'); } else { header('location wronginfo.php'); } ?> Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935322 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 $fp = fopen("registrationinfo/info.txt","a+"); $fileData = fread($fp,filesize("registrationinfo/info.txt")); fclose($file); $array = explode(':', $fileData); mode a means wrtie only, a+ means read/write. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935337 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 also, can I point out that because you're using a file with the extension .txt if somebody gueses the location and name of this file then they will be able to see all of your usernames/password in their browser in plain text, not very secure at all. you'll need a htaccess file in the same directory with the following in it (Google htaccess): IndexIgnore *.txt Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935339 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 Warning: fclose() expects parameter 1 to be resource, null given on line 8 Warning: Cannot modify header information - headers already sent by on line 18 running into a few error Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935349 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 dammit, my fauly, fclose($file); should be fclose($fp); and read the post stickied at the top of this form for headers already sent. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935352 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 however, the headers error is because you're trying to modify header info after an error has been output to the screen. fix the error and the headers error will go. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935354 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 im still getting a a empty page... ive tried fixing it with soloution's and i keep changing it and its always a blank page heres the complete code. <?php session_start(); $usernamematch = $_POST['username']; $passwordmatch = $_POST['password']; $fp = fopen("registrationinfo/info.txt","a+"); $fileData = fread($fp,filesize("registrationinfo/info.txt")); fclose($fp); $array = explode(':', $fileData); $username = trim($array[0]); //user-name $password = trim($array[1]); //password if ($username == $usernamematch & $password == $passwordmatch) { $_SESSION['loginsuccessfull'] = true; header('location indexsuccessfull.php'); } else { header('location wronginfo.php'); } ?> Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935358 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 i fixed the blank page stupid error on my behalf location url instead of being location: url im always getting wronginfo.php though now. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935360 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 ok, do me a favour, change to the following: $fp = fopen("registrationinfo/info.txt","a+"); $fileData = fread($fp,filesize("registrationinfo/info.txt")); print_r($fileData); fclose($fp); You will get the headers error again but ignore that and paste the printed data to this thread. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935370 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 test1:test2:test3:test4:test5:test6 test1 being my username test2 being my password Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935375 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 ok, now change the following section to: $username = trim($array[0]); //user-name $password = trim($array[1]); //password print_r($username); print_r($password); and check that matches what you're typing in to login. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935380 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 test1:test2:test3:test4:test5:test6 test1test2 is what was displayed and that is exactly what im typing in even copied pasted to make sure Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935382 Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 if ($username == $usernamematch && $password == $passwordmatch) Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935389 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 if ($username == $usernamematch && $password == $passwordmatch) i was thinking thats where the problem lies, and i did try that. Im still getting Whooops error meaning im being directed to the wronginfo.php page. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935394 Share on other sites More sharing options...
GKWelding Posted October 12, 2009 Share Posted October 12, 2009 do print_r on all 4 variables within the IF statement, make sure they are what you are expecting. if not then that's your problem. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935421 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 print_r($username); print_r($usernamematch); print_r($password); print_r($passwordmatch); wont work for me. could it be that $usernamematch == $username ? insted of $username == $usernamematch Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935425 Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 print_r($username); print_r($usernamematch); print_r($password); print_r($passwordmatch); wont work for me. What do you mean "wont work for me"? What are you getting? could it be that $usernamematch == $username ? insted of $username == $usernamematch By the rules of logic, A = B is the same as B = A. So $usernamematch == $username and $username == $usernamematch are identical Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935430 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 test1test2 i get that for all 4 variables. could it be that it isnt retrieveing the information from the file correctly? Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935433 Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 Modify print_r($username); print_r($usernamematch); print_r($password); print_r($passwordmatch); to echo '$username='; print_r($username); echo '<br />$usernamematch='; print_r($usernamematch); echo '<br />$password='; print_r($password); echo '<br />$passwordmatch='; print_r($passwordmatch); echo '<br />'; That way, you can see whether it's the values you're reading from the file that are wrong, or if it's the $_POST data that isn't right Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935434 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 $username=test1 $usernamematch=test1 $password=test2 $passwordmatch=test2 Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935436 Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 so its $_POST that is causing the issues... ill be having a look. $_POST['Variable'] is the correct way to post isnt it? It is the correct way, and remember that the Variable is case-sensitive... it must match the name in your html form exactly. Try putting print_r($_POST) at the top of your script. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935438 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 Ive worked it out... after all that the reason it wasnt working was because of all the r_prints & i had to change the values within post. All working now thanks for that guys. Link to comment https://forums.phpfreaks.com/topic/177390-solved-trying-to-create-a-simple-login-function/#findComment-935440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.