Jump to content

Dumb Question


ThePsion5
 Share

Recommended Posts

I feel like a n00b for asking about this, but I can't put together in my head a way for a PHP script to validate a username (through a form) and then go to a different page (or return to the previous one if the username was invalid). Can anyone point me in the right direction/lend me some code?

Link to comment
Share on other sites

this is what I use for a login script (one page)login.php

<?phpsession_start();//db.php includes the database variablesinclude('db.php');if($act == "login"){  $form_username = $_POST['username'];  $form_password = $_POST['password'];  $match = false;  $db = mysql_connect($db_server,$db_usr,$db_passw);  @mysql_select_db($db_name) or die("Unable to select database");  $sql = "sql statement to select all user records";  $data = mysql_query($sql);  $rows = mysql_numrows($data);  $i=0;  while($i < $rows)  {    $db_username = mysql_result($data,$i,'username');    $db_password = mysql_result($data,$i,'password');    if($form_username == $db_username && $form_password == $db_password)    {       $match = true;    }  }  if($match)   {    $_SESSION['username'] = $form_username;    $_SESSION['loggedin'] = "yes";    //other session info you want to log    header("Location: newPage.php");  }  else    $msg = "Invalid username or password";}?><html><head>...</head><body><form action="?act=login" method="post">...the form...</form><!--error messages--><?php echo $msg?>

I also create a authenicate.php to include on at the top of all protected pages.authenticate.php

<?phpsession_start();if($_SESSION['loggedin'] != "yes")  header("Location: login.php");?>

hope this helps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...