PDA

View Full Version : PHP login script... Heeeylp!


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

Sky
10-01-2002, 08:55 PM
Uhh ok. now, do i have to call session_start() on every page i wan't to use the browser's current (last page) session on? or just the start one?

Sky
10-01-2002, 09:07 PM
urg. it doesn't work. lookie. (not putting sstart in each page)
check it out. www.flash.slickshosting.com/mr/index.php

Sky
10-01-2002, 09:27 PM
Ok, the sessions work--but on the index only. when i try to access this file from a link on the index, i get the else message. g2g2b, ask more tommorow :D ;) ;P

<?

session_start();

$pagename = "vXphp Mindrover:the europa project [sessiontest]";

$pn2 = "[sessiontest]";

$lupdate = "1.10.2002";

require("header.php");

?>

<body bgcolor="#000000" text="#ffffff" link="#33cc33" vlink="#006633" alink="#85e685">
<font face="Tahoma, Arial, Helvetica" size="2">

<? require("logo.php");



if($_session['logink'] == "true")
{
echo "Welcome logged in person!";
}

else
{
echo "Please register or login to access this page.";
}


require("footer.php");
?>

hehe my first origional part php site! whoo! *falls asleep*

Sky
10-01-2002, 09:31 PM
o and the latest login.php('required')
<?

if($_session['logink'] == "true")
{
$logink = "true";
}

/* 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 or $_session['logink'] == "true")
{
echo "<H3>You are now logged in.</H3>";
echo "<a href='index.php?action=edit'>Edit info</a>";
echo "<a href='stest.php'>session test</a>";
}
$logink="true";
$_session['logink'] = "true";
}
if($logink==""):
error_message("Login failed, bad username/password", $username, $password);
endif;


elseif($action=="edit"):
{
echo "<FORM action='$PHP_SELF?action=change' method='post' /><br />";
echo "<P>Name: <input type='text' name='name' value='$fname' class='i' /><br />";
echo "E-mail Address: <input type='text' name='email' value='$femail' class='i' /><br />";
echo "Website Address: <input type='text' name='url' value='$furl' class='i' /><br />";
echo "Password: <input type='text' name='newpass' value='$fpass' class='i' /><br />";
echo "Current Username: <input type='text' name='username' value='$username' class='i' /><br />";
echo "Current Password: <input type='text' name='password' value='$password' class='i' /><br />";
echo "<input type='submit' value='Change'><br />";
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><br />
<a href="signup.php">New? Sign up!</a>

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

Sky
10-02-2002, 05:19 AM
Originally posted by dbindel

if($_session['logink'] == "true")

Ya, that's it. i set it at the start and where $logink is set. And would setting it all caps affect the whole site, when this only made(ya, it didin't work before) the index or same bage load the session? because now i can at least edit the profile... I'll try.

I'm not sure if this is it or not, but you should use $_SESSION[...] in ALL-CAPS. Also, make sure you're setting $_SESSION['logink'] to the string "true" and not just boolean true. You can use either one, but be consistent or it will mess up on you.
Yes, i use "true" because i've heard of people preaching about quoted values...

Good luck with your website,
David Bindel
Thanks :D

Sky
10-02-2002, 06:46 AM
Err, ok. I changes caps on both pages, and now i can go to a page, but i can't return.
( i thnk i fixed this, cause it was still stored, just no 'if' designed for still logged in. )

Now i'm getting this error on login.php:: Parse error: parse error, unexpected $ in /home/virtual/site5/fst/var/www/html/mr/login.php on line 126




<?

function loggedin($username)
{
echo "<h4>You are currently logged in as $username</h4>";
echo "<a href='index.php?action=edit'>Edit info</a><br />";
echo "<a href='stest.php'>Session test</a>";
}

function error_message($message, $username, $password)
{
echo "<H3> $message;</H3>";
echo "<form action='$PHP_SELF' method='post'>";
echo "<P>Username: <input type='text' name='username' value='<?echo $username;?>' class='i' /> /><br />";
echo "Password: <input type='password' name='password' value='<?echo $password;?>' class='i' /> /><br />";
echo "<input type='submit' value='Login' src='login.png' /></p>";
echo "</form>";
}

if($_SESSION['logink'] == "true")
{
$logink = "true";
loggedin($username);
}

/* 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 or $_SESSION['logink'] == "true")
{
loggedin($username);
}
$logink="true";
$_SESSION['logink'] = "true";
}
if($logink==""):
error_message("Login failed, bad username/password<br /> OR you haven't logged in at all.", $username, $password);
endif;


elseif($action=="edit"):
{
echo "<FORM action='$PHP_SELF?action=change' method='post' /><br />";
echo "<P>Name: <input type='text' name='name' value='$fname' class='i' /><br />";
echo "E-mail Address: <input type='text' name='email' value='$femail' class='i' /><br />";
echo "Website Address: <input type='text' name='url' value='$furl' class='i' /><br />";
echo "Password: <input type='text' name='newpass' value='$fpass' class='i' /><br />";
echo "Current Username: <input type='text' name='username' value='$username' class='i' /><br />";
echo "Current Password: <input type='text' name='password' value='$password' class='i' /><br />";
echo "<input type='submit' value='Change'><br />";
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:
{
If(!$_SESSION['logink'])
{
echo "<FORM action='<?$PHP_SELF;?>?action=login' method='post'>";
echo "<P><b>Username: </b><input type='text' name='username' class='i' /><br />";
echo "<b>Password: </b><input type='password' name='password' class='i' /><br />";
echo "<input type='submit' src='login.png' alt='Login' /></p>";
echo "<a href='signup.php'>New? Sign up!</a>";
echo "</form><br />";
}
elseif($_SESSION['logink'] == "true")
{
echo "";
}
}
LN 125 --> ?>
LN 126 -->


I just went though and fixed the indentations, and everything... wheew. and considering i don't fully get php i don't see how endif, elseif, else are used (fully)

Sky
10-02-2002, 07:04 AM
Thanks for your help db.... i don't know what i would do without you! :)

One more problem, i'm trying to functionize the 'items' on my index, you know, like jargon, basic ideas, etc. and i did this in my header.


<?

session_start();

$pagename = "vXphp Mindrover:the europa project [hub]";

require("header.php");

echo("<body text=\"white\" bgcolor=\"black\">");

function item($url, $title, $bullet, $date, $content)
{
echo "<font size='2'><img src='images/bullet_$bullet.gif' width='10' height='10' border="0" alt='Bullet'>&nbsp;&nbsp; ";
echo "<b><a href='$url'>$title</a>";
echo "</b><font size='1'>($date)</font></font><br /><font size='1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "$content</font><br /><br />";
}
?>

is that right? well it says: no. here's the error i get -- and i see no reason for it -- Parse error: parse error, unexpected T_LNUMBER, expecting ',' or ';' in /home/virtual/site5/fst/var/www/html/mr/index.php on line 13


Nevermind ^_^ i figured it out! it was the " 's in ="0" . whops. (yay i actually found the error!) still need the login-ln 126 error though :ouch: :smack:

Sky
10-02-2002, 08:08 PM
Well, for now it's working. thanks much to you :) (and i found a more active forum devoted to php so i go there for help :p) so i'll be hanging around for other questions and my hosting *looks for admin* where is he anyway?

Sky
10-06-2002, 05:23 PM
Ya... I want mi hosting... ... gahndi doesn't deserve his...