emediastudios Posted June 1, 2008 Share Posted June 1, 2008 Im just learning php and alot goes to the help i get here. I have tried and keep gettin errors when tryin to do the code myself. I can make it work with dreamweaver but i want to learn the proper way. I have this form below. <form id="login" name="login" method="post" action=""> <table width="950" border="0" align="center" cellpadding="4"> <tr> <td colspan="2" class="silver">Registered Salon Login</td> <td width="678" colspan="2" rowspan="5"> </td> </tr> <tr> <td width="123" class="contactdetails">Username:</td> <td width="117"><div align="left"> <input name="user" type="text" class="smalltext" id="user" /> </div></td> </tr> <tr> <td class="contactdetails">Password:</td> <td><div align="left"> <input name="password" type="text" class="smalltext" id="password" /> </div></td> </tr> <tr> <td colspan="2" class="login1"><input name="login2" type="submit" class="smalltext" id="login2" value="Login" /> I know its alot to ask but if some one could write me the code that makes this work, i'd be stoked. I promise to study it and learn to do it myself. I created a table called salon, in my database. and i connect to the database through a include file. I have a username and password field in that table. How do i check the entries against each other.? Once they enter the right details i want them to be sent to a file called templates.php Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 1, 2008 Share Posted June 1, 2008 $config_basedir = 'http://www.yoursite.com'; if($_POST['login']) { $loginsql = "SELECT * FROM users WHERE username= '" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "'"; $loginres = mysql_query($loginsql); $numrows = mysql_num_rows($loginres); if($numrows == 1){ $loginrow = mysql_fetch_assoc($loginres); if($loginrow['status'] == 1) { session_register("SESS_LOGGEDIN"); session_register("SESS_EMAIL"); session_register("SESS_USERID"); session_register("SESS_USERNAME"); $_SESSION['SESS_LOGGEDIN'] = 1; $_SESSION['SESS_EMAIL'] = $loginrow['email']; $_SESSION['SESS_USERID'] = $loginrow['id']; $_SESSION['SESS_USERNAME'] = $loginrow['username']; header("Location: " . $templates.php); } else { header("Location: " . $config_basedir ."?error=verified"); } } else { header("Location: " . $config_basedir ."?error=incorrect"); } } Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 1, 2008 Author Share Posted June 1, 2008 Thanks heaps But i still can get it to work. I Changed my form to read <form id="login2" name="login2" method="post" action="login_salon.php"> <table width="950" border="0" align="center" cellpadding="4"> <tr> <td colspan="2" class="silver">Registered Salon Login</td> <td width="678" colspan="2" rowspan="5"> </td> </tr> <tr> <td width="123" class="contactdetails">Username:</td> <td width="117"><div align="left"> <input name="username" type="text" class="smalltext" id="username" /> </div></td> </tr> <tr> <td class="contactdetails">Password:</td> <td><div align="left"> <input name="password" type="text" class="smalltext" id="password" /> </div></td> </tr> <tr> <td colspan="2" class="login1"><input name="login2" type="submit" class="smalltext" id="login2" value="Login" /></td> </tr> <tr> <td colspan="2"> </td> </tr> </table> <span class="smalltext">Not a registered Salon?<br /> Simply fill out the form below to register, and we will send you your login details.</span> <br /> <br /> </form> and my process file code <?PHP $config_basedir = 'salons.php'; include('includes/include.php'); if($_POST['login2']) { $loginsql = "SELECT * FROM salon WHERE username= '" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "'"; $loginres = mysql_query($loginsql); $numrows = mysql_num_rows($loginres); if($numrows == 1){ $loginrow = mysql_fetch_assoc($loginres); if($loginrow['status'] == 1) { session_register("SESS_LOGGEDIN"); session_register("SESS_EMAIL"); session_register("SESS_USERID"); session_register("SESS_USERNAME"); $_SESSION['SESS_LOGGEDIN'] = 1; $_SESSION['SESS_EMAIL'] = $loginrow['email']; $_SESSION['SESS_USERID'] = $loginrow['id']; $_SESSION['SESS_USERNAME'] = $loginrow['username']; header("Location: " . $templates.php); } else { header("Location: " . $config_basedir ."?error=verified"); } } else { header("Location: " . $config_basedir ."?error=incorrect"); } } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 1, 2008 Author Share Posted June 1, 2008 I dont get any errors, it just does nothing. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 1, 2008 Share Posted June 1, 2008 Try this <?PHP $config_basedir = 'salons.php'; include('includes/include.php'); if($_POST['login2']) { $loginsql = "SELECT * FROM salon WHERE username= '" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "'"; $loginres = mysql_query($loginsql); $numrows = mysql_num_rows($loginres); if($numrows == 1){ $loginrow = mysql_fetch_assoc($loginres); if($loginrow['status'] == 1) { session_register("SESS_LOGGEDIN"); session_register("SESS_EMAIL"); session_register("SESS_USERID"); session_register("SESS_USERNAME"); $_SESSION['SESS_LOGGEDIN'] = 1; $_SESSION['SESS_EMAIL'] = $loginrow['email']; $_SESSION['SESS_USERID'] = $loginrow['id']; $_SESSION['SESS_USERNAME'] = $loginrow['username']; header("Location: " . $templates.php); } } else { header("Location: " . $config_basedir ."?error=incorrect"); } } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 1, 2008 Author Share Posted June 1, 2008 Gives me a blank page on submit ??? Quote Link to comment Share on other sites More sharing options...
helraizer Posted June 1, 2008 Share Posted June 1, 2008 To the top of any of your php pages, add error_reporting(E_ALL); ini_set('display_errors', true); see if you get any errors. Sam Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 1, 2008 Author Share Posted June 1, 2008 Yeah just did that error below Notice: Undefined index: login2 in C:\Program Files\Apache Group\Apache2\htdocs\salondigitalentertainment\login_salon.php on line 6 Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 1, 2008 Author Share Posted June 1, 2008 Notice: Undefined index: status in C:\Program Files\Apache Group\Apache2\htdocs\salondigitalentertainment\login_salon.php on line 15 This line if($loginrow['status'] == 1) { do i need this email stuff? session_register("SESS_EMAIL"); Quote Link to comment Share on other sites More sharing options...
timmah1 Posted June 2, 2008 Share Posted June 2, 2008 sets the sessions for the login. You could just use session_register("SESS_LOGGEDIN"); session_register("SESS_USERID"); session_register("SESS_USERNAME"); $_SESSION['SESS_LOGGEDIN'] = 1; $_SESSION['SESS_USERID'] = $loginrow['id']; $_SESSION['SESS_USERNAME'] = $loginrow['username']; Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 2, 2008 Author Share Posted June 2, 2008 OK i got it to work. I just need one thing to perfect it. I have an error message display if the fields are left blank. But how do i add an error message if the username or password is not the right one. Here is the amended code. \ <?php include('includes/include.php'); include('form_email_config.php'); #Form has been submitted? if((isset($_POST['login'])) AND ($_POST['login'] == 'Login')){ ob_start(); $host="localhost"; // Host name $username="root"; // Mysql username $password="5050888202"; // Mysql password $db_name="sde"; // Database name $tbl_name="salon"; // Table name $errors_login = array(); #Initiate error variable #Check for blanks and clean data if(empty($_POST['username'])) $errors_login[] = 'Please put in your username.'; else $clean['username'] = htmlspecialchars($_POST['username']); if(empty($_POST['password'])) $errors_login[] = 'Please put in your password.'; else $clean['password'] = htmlspecialchars($_POST['password']); // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $username and $password $username=$_POST['username']; $password=$_POST['password']; // To protect MySQL injection (more detail about MySQL injection) $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $username and $password, table row must be 1 row if($count==1){ // Register $username, $password and redirect to file "login_success.php" session_register("username"); session_register("password"); header("location:templates.php"); } else { ob_end_flush(); } } Quote Link to comment Share on other sites More sharing options...
emediastudios Posted June 2, 2008 Author Share Posted June 2, 2008 I used a differnt code i got off the net, made some alterations and works fine, just want to add that extra feature noted above Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.