First i will make a login form.
<form method="POST" action="login.php">Username: <input type="text" name="userid" >Password: <input type="password" name="password" ><input type="submit" value="login" name="login"></form>
Now lets have a look at login.php
<?PHP//isset function checks for exsistence of the variable if isset returns false then it redirects to login pageif (!isset($userid) || !isset($password)) {header( "Location: http" address of your login page " );}//here it checks if forms fields are submitted empty or not//if empty it redirects to login pageelseif (empty($userid) || empty($password)) {header( Location:"address of login page" );}else{//addslashes() function is used to escape double quotes ("), single quotes ('), backslash (\) or NULL.It does so by automatically inserting a backslash (\) character in front of the characters in a string.//addslashes() function is especially useful to use on a string before inserting into a database$user = addslashes($_POST['userid']);$pass = $_POST['password'];//Store the form value into php variables$dbHost = "localhost";$dbUser = "Database userid";$dbPass = "Database password ";//as assumed earlies we have a login database$dbDatabase = "login";//conneting to the database$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error while connecting to database.");mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");$result=mysql_query("select * from users where username='$userid' AND password='$password'", $db);//checks if result is not empty$row = mysql_num_rows($result);if($row > 0){while($row = mysql_fetch_array($result)){//A session is started and variable registeredsession_start();session_register('userid');//the session variable userid is used through out the users sessionecho 'Successful login !';//redirect to a page//let check.php be the page where you want to redirectheader( "Location: http:"check.php");}}else {echo 'There was an error logging in please try again.';}?>
Please check the next post where i will discuss about how to check if user is logged in.


0 comments until now.
Post a Comment