Sky
10-01-2002, 08:20 PM
Hehe i really need to just learn php all the way... but it's a work in progress. here's a login script. or the login part i 'require' into my main page. (subject to change) I know it's not secure, i don't care because nothing sensitive relys on it. :D so it works for me...But i want it to store the 'logged in' value (till the user exit the browser) in a cookie so the rest of the site can see that. how would i go about that? (would that work? ima iddiot royale :D )
ANYHOO here's the code.... .
<?
/* Simple Signup/Login script
Created on: Feburary 22, 2001
Copyright© 123 Make Me! All Rights Reserved.
To get this script, goto http://123-makeme.com
This Software is free and can be modified and redistributed
under the condition that the copyright and information above
stays intact and with the script.*/
#User Variables
$datafile="/home/virtual/site5/fst/var/www/html/mr/data.txt";
#End User Variales
#If the call for the script is to log them in, DO IT!
if($action == "login"):
#This makes sure all fields are filled out.
if((!$username)or(!$password))
{
#If there is one missing, send them to the error.
error_message("One or more required fields were left blank!", $username, $password);
}
#Open the datafile and login the user.
$file=file($datafile);
while(list(,$value)=each($file))
{
list($fname,$femail,$furl,$fuser,$fpass,$blank)=split( "\|", $value);
if($username==$fuser && $password==$fpass)
{
echo "<H3>You are now logged in.</H3>";
echo "<a href='index.php?=edit'>Edit info</a>";
}
$logink="1";
}
if($logink==""):
error_message("Login failed, bad username/password", $username, $password);
endif;
elseif($action=="edit"):
{
echo "<FORM action='$PHP_SELF?action=change' method='post' style='color: #33cc33; background-color: #000000;' />";
echo "<P>Name: <input type='text' name='name' value='$fname' style='color: #33cc33; background-color: #000000;' />";
echo "E-mail Address: <input type='text' name='email' value='$femail' style='color: #33cc33; background-color: #000000;' />";
echo "Website Address: <input type='text' name='url' value='$furl' style='color: #33cc33; background-color: #000000;' />";
echo "Password: <input type='text' name='newpass' value='$fpass' style='color: #33cc33; background-color: #000000;' />";
echo "Current Username: <input type='text' name='username' value='$username' style='color: #33cc33; background-color: #000000;' />";
echo "Current Password: <input type='text' name='password' value='$password' style='color: #33cc33; background-color: #000000;' />";
echo "<input type='submit' value='Change'>";
echo "</p>";
echo "</FORM>";
}
elseif($action=="change info"):
#This makes sure all fields are filled out.
if((!$name)or(!$email)or(!$url)or(!$username)or(!$password)){
#If there is one missing, send them to the error.
error_message("One or more required fields were left blank! Please re- login.", $username, $password);
}
#Open the datafile and login the user.
$file=file($datafile);
while(list(,$value)=each($file)){
list($fname,$femail,$furl,$fuser,$fpass)=split( "\|", $value);
if($username==$fuser && $password==$fpass){
$oldword="$fname|$femail|$furl|$fuser|$fpass|";
$newword="$name|$email|$url|$username|$newpass|";
$fp = fopen($datafile, "r");
$data = fread($fp, filesize($datafile));
fclose($fp);
$newdata = str_replace($oldword, $newword, $data);
$fp = fopen($datafile,"w");
fwrite($fp,$newdata) or die ("error writing");
fclose($fp);
$succ = "1";
echo "Everything was changed successfully! <A HREF=\"$PHP_SELF?action=login&username=$username&password=$newpass\">Edit Again</ A>";
}
}
if(!$succ):
error_message("Login failed, bad username/password", $username, $password);
endif;
else:?>
<FORM action="<?$PHP_SELF;?>?action=login" method="post">
<P><b>Username: </b><input type="text" name="username" style="color: #33cc33; background-color: #000000;" /><br />
<b>Password: </b><input type="password" name="password" style="color: #33cc33; background-color: #000000;" /><br />
<input alt="Login" src="login.png" type="submit" /></p>
</FORM>
<?endif;
function error_message($message, $username, $password){?>
<H3><?echo $message;?></H3>
<FORM action="<?$PHP_SELF;?>" method="post">
<P>Username: <input type="text" name="username" value="<?echo $username;?>" style="color: #33cc33; background-color: #000000;" /> /><br />
Password: <input type="password" name="password" value="<?echo $password;?>" style="color: #33cc33; background-color: #000000;" /> /><br />
<input type="submit" value="Login" src="login.png" /></p>
</FORM>
<?exit;
}?>
any suggestions welcome....
ANYHOO here's the code.... .
<?
/* Simple Signup/Login script
Created on: Feburary 22, 2001
Copyright© 123 Make Me! All Rights Reserved.
To get this script, goto http://123-makeme.com
This Software is free and can be modified and redistributed
under the condition that the copyright and information above
stays intact and with the script.*/
#User Variables
$datafile="/home/virtual/site5/fst/var/www/html/mr/data.txt";
#End User Variales
#If the call for the script is to log them in, DO IT!
if($action == "login"):
#This makes sure all fields are filled out.
if((!$username)or(!$password))
{
#If there is one missing, send them to the error.
error_message("One or more required fields were left blank!", $username, $password);
}
#Open the datafile and login the user.
$file=file($datafile);
while(list(,$value)=each($file))
{
list($fname,$femail,$furl,$fuser,$fpass,$blank)=split( "\|", $value);
if($username==$fuser && $password==$fpass)
{
echo "<H3>You are now logged in.</H3>";
echo "<a href='index.php?=edit'>Edit info</a>";
}
$logink="1";
}
if($logink==""):
error_message("Login failed, bad username/password", $username, $password);
endif;
elseif($action=="edit"):
{
echo "<FORM action='$PHP_SELF?action=change' method='post' style='color: #33cc33; background-color: #000000;' />";
echo "<P>Name: <input type='text' name='name' value='$fname' style='color: #33cc33; background-color: #000000;' />";
echo "E-mail Address: <input type='text' name='email' value='$femail' style='color: #33cc33; background-color: #000000;' />";
echo "Website Address: <input type='text' name='url' value='$furl' style='color: #33cc33; background-color: #000000;' />";
echo "Password: <input type='text' name='newpass' value='$fpass' style='color: #33cc33; background-color: #000000;' />";
echo "Current Username: <input type='text' name='username' value='$username' style='color: #33cc33; background-color: #000000;' />";
echo "Current Password: <input type='text' name='password' value='$password' style='color: #33cc33; background-color: #000000;' />";
echo "<input type='submit' value='Change'>";
echo "</p>";
echo "</FORM>";
}
elseif($action=="change info"):
#This makes sure all fields are filled out.
if((!$name)or(!$email)or(!$url)or(!$username)or(!$password)){
#If there is one missing, send them to the error.
error_message("One or more required fields were left blank! Please re- login.", $username, $password);
}
#Open the datafile and login the user.
$file=file($datafile);
while(list(,$value)=each($file)){
list($fname,$femail,$furl,$fuser,$fpass)=split( "\|", $value);
if($username==$fuser && $password==$fpass){
$oldword="$fname|$femail|$furl|$fuser|$fpass|";
$newword="$name|$email|$url|$username|$newpass|";
$fp = fopen($datafile, "r");
$data = fread($fp, filesize($datafile));
fclose($fp);
$newdata = str_replace($oldword, $newword, $data);
$fp = fopen($datafile,"w");
fwrite($fp,$newdata) or die ("error writing");
fclose($fp);
$succ = "1";
echo "Everything was changed successfully! <A HREF=\"$PHP_SELF?action=login&username=$username&password=$newpass\">Edit Again</ A>";
}
}
if(!$succ):
error_message("Login failed, bad username/password", $username, $password);
endif;
else:?>
<FORM action="<?$PHP_SELF;?>?action=login" method="post">
<P><b>Username: </b><input type="text" name="username" style="color: #33cc33; background-color: #000000;" /><br />
<b>Password: </b><input type="password" name="password" style="color: #33cc33; background-color: #000000;" /><br />
<input alt="Login" src="login.png" type="submit" /></p>
</FORM>
<?endif;
function error_message($message, $username, $password){?>
<H3><?echo $message;?></H3>
<FORM action="<?$PHP_SELF;?>" method="post">
<P>Username: <input type="text" name="username" value="<?echo $username;?>" style="color: #33cc33; background-color: #000000;" /> /><br />
Password: <input type="password" name="password" value="<?echo $password;?>" style="color: #33cc33; background-color: #000000;" /> /><br />
<input type="submit" value="Login" src="login.png" /></p>
</FORM>
<?exit;
}?>
any suggestions welcome....