Jump to content

[SOLVED] Login help


emediastudios

Recommended Posts

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

Link to comment
Share on other sites


$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");
		}
	}

Link to comment
Share on other sites

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");
		}
	}
	?>

 

Link to comment
Share on other sites

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");

}

}

?>

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

OK i got it to work. ;D

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();
}
}

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.