Jump to content

simple login script


maxic0

Recommended Posts

Hi, im creating a simple login script and i dont have a problem yet but im wondering if there is a way that i can improve this bit of code, this is what i have for validating the fields...

 

<?php


if (!isset($username) {
echo "Username is a required field. <BR/> Please go <A HREF='register.php'>back</A> and fill it in.";

}

?>

 

I have four of these bits of code for validating the username, password, confirm password and email.

 

If there a way that i could use one if statement to validate all the fields then if one or more fields are left blank, it will say that they are required fields and say what ones they have missed out?

 

Thanks.

Link to comment
Share on other sites

no... each one needs to be validated seperatly...

if(empty($_POST[username]) echo "Username is a required field. <BR/> Please go <A HREF='register.php'>back</A> and fill it in.";
elseif(empty($_POST[password]) echo "Password is a required field. <BR/> Please go <A HREF='register.php'>back</A> and fill it in.";
elseif(empty($_POST[email]) echo "Email is a required field. <BR/> Please go <A HREF='register.php'>back</A> and fill it in.";

and so on :-)

Link to comment
Share on other sites

personally... i prefer the less specific ones...

 

$username=addslashes(strip_tags($_POST[username]));
$password=md5($_POST[username]);
$query=mysql_query("SELECT * FROM `accounts` WHERE `username`='$username' AND `password`='$password' LIMIT 1");
$row=mysql_fetch_array($query);
if(empty($row)){
$_SESSION[error]='Invalid username/password';
redirect();
}else{
$_SESSION[user]=$row;
redirect();
}

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.