Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Login in php

login

  • Please log in to reply
3 replies to this topic

#1 xle_camry

xle_camry

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 170 posts

Posted 03 February 2012 - 10:18 AM

Dear friends,

As I am new to PHP, this question may be very simple to you.

So, please be tolerant and help me.

This is my question:

1. I have table called "users" in my mysql database. Database called "people".
2. I have two users in my "users" table. They are:

a) username: admin
password: secret

B) username: user
password: access

3. I have 3 php files: login.php, admin.php, user.php

4. Firstly opens login.php and there asked to enter username and password.

5. So, how can I do the followings:

if administrator types its username and password correctly, it should enter admin.php

if user types its username and password correctly, it should enter user.php

if neither administrator nor user is right, it must be message in javascript: Check your username and password!

Any advices?

Thanks!
  • 0

#2 logicPwn

logicPwn

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 177 posts
  • Location:Fresno, CA
  • Programming Language:C#, PHP, JavaScript, PL/SQL

Posted 03 February 2012 - 11:04 PM

Ok to do this your going to need one page that has HTML form and one PHP processing page.

In your HTML page

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Login Example</h3>
<form action="login.php" method="post">
<input type="text" id="username" name="username" /><br />
<input type="password" id="password" name="password" /><br />
<input type="submit" value="Login">
</form>
</body>
</html>


and in login.php script

<?php
$user = array();
$user['username'] = $_POST['username']; //add extra checking
$user['password'] = $_POST['password']; //to make sure there not empty

// Connect to DB and select table
$con = mysql_connect("host", "username", "password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("people", $con);

// Run query to check if user exists
$result = mysql_query("SELECT * FROM users WHERE username='" . $user['username'] . "'");
if (!$result) {
die("No user exists"); // switch to header("Location:url");
}
$userdata = mysql_fetch_row($result);

// Check password
if ($user['password'] == $userdata['password']) {
//session or cookies
header("Location:url");
}
else {
header("Location:url"); // Password did not match one in DB
}
?>


This script that I made for you is very basic. You should always escape user inputted data before running queries.

Edited by logicPwn, 04 February 2012 - 04:26 PM.

  • 0

#3 xle_camry

xle_camry

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 170 posts

Posted 04 February 2012 - 10:33 AM

You are inserting username and password into the table.
I dont want this.

I already have that username and password in my database.
I want that if username and password is correct, then that person can login into that page.
  • 0

#4 logicPwn

logicPwn

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 177 posts
  • Location:Fresno, CA
  • Programming Language:C#, PHP, JavaScript, PL/SQL

Posted 04 February 2012 - 04:21 PM

Your right. Here's the code for checking login data.

// Run query to check if user exists
$result = mysql_query("SELECT * FROM users WHERE username='" . $user['username'] . "'");
if (!$result) {
die("No user exists"); // switch to header("Location:url");
}
$userdata = mysql_fetch_row($result);

// Check password
if ($user['password'] == $userdata['password']) {
//session or cookies
header("Location:url");
}
else {
header("Location:url"); // Password did not match one in DB
}


Changed in the above post also so it's a full page example.
  • 0





Also tagged with one or more of these keywords: login

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download