Jump to content

Can someone help me with this? Please.


tahj

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :D

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

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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 !

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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