tahj Posted November 13, 2007 Share Posted November 13, 2007 How do I do this? Thanks. You are required to write an application that ensures security to a hosting server through user’s authentication. The client application (HTML file) consists of a user’s log in window that contains two text boxes: one for the ‘username’ and the other for the ‘password’, a check box with the text ‘New User’ right next to it, and two buttons: ‘Log In’, and ‘Clear Information’. The server file (PHP file) is to validate all the data sent to the server by the user after pressing the ‘Log In’ button before any processing is done. At the server side, create a one-dimension array with 10 records of 5 users. The odd elements of the array consists of the passwords of the users, and the even elements the username of the users. So, array element 0 (zero) corresponds to the value of the first username, and array element 1 corresponds to the first user password, and so on. After validation of the input data, send a message to the user either acknowledging the existence of such user in the array or informing to the user that there is no such user with the data that had been sent over to the server (in case the user did not mark the ‘New User’ check box). In case the check box ‘New User’ is “ON” (marked) then verify if the username and password are indeed not in the array – in this case, make the two data (username and password) as the new elements of the array (insert them at the bottom of the array). Then send to the user a message informing him/her of the addition of his/her data to the system. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 13, 2007 Share Posted November 13, 2007 Learn a lot of PHP. PhREEEk Quote Link to comment Share on other sites More sharing options...
grejon04 Posted November 13, 2007 Share Posted November 13, 2007 I would start by learning about sessions. Is this your homework? :-) Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 13, 2007 Share Posted November 13, 2007 search this forum and google for "user login with php and mysql" and go to www.php.net and learn about arrays there. Hope this is not due tomorrow - LOL . Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 13, 2007 Share Posted November 13, 2007 that isn't possible to do without rewriting the file, adn it sounds like homework you should learn to do, you don't need sessions or any of that just get the key to a matching username and then if(next($array) == $_POST['password']) its a match, otherwise its not, but you can't "rewrite" that array without doing a lot more complex stuff. Quote Link to comment Share on other sites More sharing options...
tahj Posted November 13, 2007 Author Share Posted November 13, 2007 Yeah, its homework that is due tomorrow. :-\ Quote Link to comment Share on other sites More sharing options...
AndyB Posted November 13, 2007 Share Posted November 13, 2007 So who get the marks if we write your script? How much will you learn if you write zero lines of code? We'll help, but we don't take requests. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 13, 2007 Share Posted November 13, 2007 "how long have you had to do this?" - that's what my parents and grandparents always used to ask me when I waited til the last minute to do this much homework. Quote Link to comment Share on other sites More sharing options...
tahj Posted November 13, 2007 Author Share Posted November 13, 2007 This is the first time I am learning PHP. Don't really like it, since I am not into programming. Programming frustrates me, since it takes so much patients to improve the coding. Its a class requirement though. Quote Link to comment Share on other sites More sharing options...
jimlawrnc Posted November 13, 2007 Share Posted November 13, 2007 I learned alot from http://hudzilla.org/phpwiki/index.php?title=Main_Page Quote Link to comment Share on other sites More sharing options...
tahj Posted November 13, 2007 Author Share Posted November 13, 2007 Ok. Thanks. Quote Link to comment Share on other sites More sharing options...
khalidorama Posted November 13, 2007 Share Posted November 13, 2007 Hi Tajh, On the contrary !. I love programming. It is like a passion for me , programming . So, I decided to think of your problem and write super perfect code just for you !. But hey, afterwards , you have to tweak your mind a little , then you will find solutions for your problems. I spent much time on this code, so you have to thank me . hehe Here is the first php file that you have to create. Just copy past the code to a file , and name it functions.php <?php include("functions.php"); $mixed = loadarray(); $username = $_GET['username']; $password = $_GET['password']; for($i=0;$i<count($mixed);$i++) { if (fmod($i,2) == 0) $registeredusers[$i] = $mixed[$i]; } if (in_array($username,$registeredusers)) { echo "The user exists"; } else { array_push($mixed,$username,$password); savearray(count($mixed),$mixed); print_r($mixed); echo "The user did not exist , however it has been created"; } ?> Here is the second file to be created in a second file <?php include("functions.php"); $mixed = loadarray(); $username = $_GET['username']; $password = $_GET['password']; for($i=0;$i<count($mixed);$i++) { if (fmod($i,2) == 0) $registeredusers[$i] = $mixed[$i]; } if (in_array($username,$registeredusers)) { echo "The user exists"; } else { array_push($mixed,$username,$password); savearray(count($mixed),$mixed); print_r($mixed); echo "The user did not exist , however it has been created"; } ?> Sadly at the end I tested my the above codes. It worked great !. However, I don't know why It needs two registerations for the same username in order to inform the user is already been registered. I don't have much time now. I will revise the code later for you. But i think there is something wrong with the first element in $registeredusers array. Regards Quote Link to comment Share on other sites More sharing options...
khalidorama Posted November 13, 2007 Share Posted November 13, 2007 HI, I made a mistake in the first code. Here is the code to be saved as functions.php <?php function savearray($arraylength,$tobesavedarray) { $fp = fopen("arraycontent.txt","w"); if(!$fp) { echo "Error! The file array could not be loaded"; } for ($i=0;$i<$arraylength;$i++) { $data = $tobesavedarray[$i]; fwrite($fp,$data."@"); } } function loadarray() { $handle = fopen("arraycontent.txt","r"); $contents = fread($handle, filesize("arraycontent.txt")); $mixed = explode("@",$contents); //@ will be put between usernames just to enable return $mixed; } ?> Quote Link to comment Share on other sites More sharing options...
tahj Posted November 13, 2007 Author Share Posted November 13, 2007 I tried the code, and it doesn't work. Do I need an HTML form too to do the inputs? Quote Link to comment Share on other sites More sharing options...
khalidorama Posted November 13, 2007 Share Posted November 13, 2007 Yep sure you need HTML file to login. I made some changes in the code. and here is the newer version save this code under login.php <?php include("functions.php"); $mixed = loadarray(); sarabiccname = $_GET['username']; UNIficATion = $_GET['password']; for($i=0;$i<count($mixed);$i++) { if (fmod($i,2) == 0) $registeredusers[$i] = $mixed[$i]; } if (in_array($username,$registeredusers)) { echo "You logged in succefully"; echo "<BR>"; echo "<a href=\"logout.php\">Click here to log out</a>"; } else { array_push($mixed,$username,$password); savearray(count($mixed),$mixed); echo "The user did not exist , however it has been created and logged in."; echo "<BR>"; echo "<a href=\"logout.php\">Click here to log out</a>"; } ?> and save this code under logout.php <?PHP session_start(); session_destroy(); echo "You have been successfully logged out"; echo "<BR>"; echo "<a href=\"test.html\">Click here to login again</a>"; ?> keep the functions.php intact, i did not change it. and this code under test.html <html> <body> <form action="login.php" name="test"> <input type="text" name="username"><br> <input type="password" name="password"><br> <input type="submit" value="click to login"> </form> </body> </html> So whenever you try to login using test.html , if the user does not exsist in the file then it is added automatically to it . Great , isn't it ! Quote Link to comment Share on other sites More sharing options...
tahj Posted November 13, 2007 Author Share Posted November 13, 2007 Thanks. I will update it. Quote Link to comment Share on other sites More sharing options...
greengo Posted November 13, 2007 Share Posted November 13, 2007 do u think it is possible do the same code without using a txt file but just an array to store the data? Quote Link to comment Share on other sites More sharing options...
khalidorama Posted November 14, 2007 Share Posted November 14, 2007 Hi, It is possible , and in this case you have to use sessions as you need to save the array's key as session variable so that if the script is run by two separate users it would be viewed to the second one what the changes of the first user. <? .... $i=0; //$i is used to save to a different location in the array $array_to_save_data_insteadof_txt_file[$i]; $i++; .... ?> the fact that $i is incremented later does not mean that if the script is run by a separate user it will start by taking the last viewed value by the first user.Instead, new variable $i will be created in the web server to the handle the first user and hence its value will be 0. so in order to solve the issue , you have to register $i as session variable and make sure that the session in started in the beginning of the script .. <? start_session(); .... //$i is used to save to a different location in the array if (isset($_session['i'])) $i = $_session['i'] else $i =0; $array_to_save_data_insteadof_txt_file[$i]; $i++; $_session['i']= $i; ..... ?> Try the above code as i don't have much time to test it . Reply to me in case you need further help. Quote Link to comment Share on other sites More sharing options...
grejon04 Posted November 14, 2007 Share Posted November 14, 2007 I don't think you have to revise the code for him anymore - i think whatever you already gave him was turned in for his assignment. I admire your desire to help, but he didn't learn a damn thing from his assignment. That's not a good thing. j Quote Link to comment 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.