ThePsion5 Posted February 13, 2006 Share Posted February 13, 2006 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 More sharing options...
aspnetguy Posted February 13, 2006 Share Posted February 13, 2006 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 More sharing options...
ThePsion5 Posted February 13, 2006 Author Share Posted February 13, 2006 Ah hah, that does help actually! I just needed to be reminded of the "location" expression. Thanks for the help. Link to comment Share on other sites More sharing options...
ThePsion5 Posted February 13, 2006 Author Share Posted February 13, 2006 Hm...i'm working on an attempt similar to that, but when I have the statement "include('db.php')" it tells that the header file has already been modified at the line where I have the header("Location: main.php") statement. Is there something i'm missing? Link to comment Share on other sites More sharing options...
aspnetguy Posted February 13, 2006 Share Posted February 13, 2006 please post your code...I can give you a better,quicker answer if I know exactly what you are doing. Link to comment Share on other sites More sharing options...
Armed Rebel Posted February 15, 2006 Share Posted February 15, 2006 You can't have any output messages before functions like that are called. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now