Programing Examples

0 comments

Shown below are some of the PHP/MySQL Samples I have created over the past few years/months/days.

Some are simple login/registration scripts, others allow simple guestbook functionality. All will show examples of how to interact with a MySQL database and encrypting data to store in the database (passwords for example).

The examples are not “live” So you can’t post to the guestbooks, but you can see the code behind the examples.

Sample Login System

This code will take the information provided in the html login form, encrypt the password field using a secret SALT and compare the password to one encrypted in the database..

// Check if session exists - if not - start one
if (!isset($_SESSION)) {
session_start();
}
// Check if we are coming back to the page after user has submitted the form
if ( 'POST' == $_SERVER['REQUEST_METHOD'] )
{
//User has tried to login - so lets pull the information from the form fields
if(isset($_POST['username'])) $username=$_POST['username'];
if(isset($_POST['password'])) $password=$_POST['password'];
/*Now we have the information from the form, we will need to excrypt the
password field - so it should match the information stored in the database */
$password = md5($password); //encrypt with MD5
/* Now we will need to connect to the database and select from the database
where the username field matches the database field */

$dbhost = 'localhost'; //enter your correct server
$dbuser = 'root'; //enter your correct username
$dbpass = 'password'; //enter your correct password

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                          ('Error connecting to mysql');

$dbname = 'petstore'; //entet your correct database name
mysql_select_db($dbname);

$sql = "select * from users where username='$username' and password='$password'";
$result = mysql_query($sql);
if (mysql_num_rows($result)!= 1) {
$error = "Login failed - Did you enter the correct details";
include "loginform.php";
} else {
$_SESSION['username'] = "$username";  // set session username
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; //set session IP
// any other data needed to navigate the site or
// to authenticate the user can be added here
// could redirect to a member page or include a page with member information
}
}
?>




Username
Password


Posted by Vincent   @   1 September 2010 0 comments

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Cloud Sky designed by Best Wordpress Themes