06 May, 2013
PHP MySQLi secure login page with session set
I'll be teaching you how to make a secure login page with PHP with simple mysqli connection and will set variable to return error on the form. I'll set session that'll save your login credentials and login expiration time.
<?php
define("DB_HOST","your-mysql-hostname");
define("DB_USER","your-mysql-account-username");
define("DB_PASS","your-mysql-account-password");
define("DB_NAME","your-mysql-database-name");
// CONNECT TO MYSQLI
$sqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_errno());
// FETCH DATA FROM FORM USING METHOD POST
// IF BUTTON NAME "LOGIN" IS SET
if (isset($_POST['login'])) {
// FETCH DATA FROM INPUT FIELD
$user = mysqli_real_escape_string($sqli, $_POST['user']);
$pass = mysqli_real_escape_string($sqli, $_POST['pass']);
// CHECK ALL FIELD HAS BEEN FILLED UP
if ($user && $pass) {
// QUERY FROM DATABASE
$query= mysqli_query($sqli, "SELECT * FROM member WHERE user='".$user."'");
$checkuser= mysqli_num_rows($query);
// CHECK IF USERNAME EXIST ON DATABASE
if($checkuser != 1) {
// I'LL BE SETTING A VARIABLE IF YOUR DOESN'T EXIST
$error = "Username doesn't exist in our database!";
}
// FETCHING PASSWORD IN DATABASE WHERE USERNAME COINCIDES
while ($row = mysqli_fetch_array($user)) {
$checkpass= $row['pass'];
// CHECK IF ENTERED PASSWORD MEETS THE USERNAME PASSWORD
if ($pass== $checkpass) {
// IF ALL OKAY SET SESSION
setcookie("user", $user, time()+7200);
$_SESSION['user'] = $user;
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (60 * 60 * 60);
header("Location: ".$_SERVER['PHP_SELF']);
exit();
} else {
// SET VARIABLE THAT'LL SHOW IF USER PASSWORD IS INCORRECT
$error = "Incorrect password!";
}
}
} else {
// SET VARIABLE IF ALL FIELD ARE NOT FILLED UP
$error = "Please enter a username and password.";
}
}
?>
<div style="text-align: center;">
<span class="error"><?php if(isset($error)) { echo $error; } ?></span>
<form action="" method="post" id="loginForm">
<span class="input">Username: <input type="text" name="user" maxlength="16"></span>
<span class="input">Password: <input type="password" name="pass"
12 April, 2013
PHP MySQLi - MySQL Improved connection
Since MySQL extension is deprecated and will be removed as of PHP 5.5 onwards, I'll be teaching you how to connect to a mysql database using the new method, MySQL Improved or MySQLi. I'll be writing 2 methods on how to connect and query on MySQLi.
1) Simple connection
//First I'll define variables
define("HOST","YOUR_MYSQL_HOST");
define("USER","YOUR_MYSQL_USERNAME");
define("PASS","YOUR_MYSQL_PASSWORD");
define("DBASE","YOUR_MYSQL_DATABASE");
09 April, 2013
PHP dynamic title
It is useful when you optimize you site for SEO, this tutorial will show you how to keep in one single page which you will include in all others, the dynamic titles script for different pages. We will need to make a variable to get the page, and put an switch & case feature to estabilish the title, then we’ll print the resulted title.
<?php
$page = $_SERVER['PHP_SELF'];
if(isset($page)) {
switch($page) {
case "index.php":
$title = "this is homepage";
break;
case "products":
$title = "products";
break;
case "contact":
$title = "contact";
break;
}
}else{
$title = "default title";
}
print "<title>$title</title>";
?>
Copy and paste it on your page header.
<?php
$page = $_SERVER['PHP_SELF'];
if(isset($page)) {
switch($page) {
case "index.php":
$title = "this is homepage";
break;
case "products":
$title = "products";
break;
case "contact":
$title = "contact";
break;
}
}else{
$title = "default title";
}
print "<title>$title</title>";
?>
Copy and paste it on your page header.
26 March, 2013
PHP MySQL simple registration form
Let's start with easy PHP sample,
simple registration page
first you need to create a registration form page.
let's name it "registrationForm.php"
<form method="POST" action="register.php">
<table>
<tr><td>Username:</td><td><input type="text" name="username"></td></tr>
<tr><td>Username:</td><td><input type="text" name="username"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td colspan="2"><input type="submit" name="register" value="register"></td></tr>
</table>
</form>
PHP MySQL login page
Now lets create a login page
First we create a login form and name it "login.php" and using post as a method
<form action="checkLogin.php" method="post">
<table border="0">
<tr>
<td colspan=2><h1>Login</h1></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username" maxlength="40"></td>
</tr>
<tr>
<td>Password:</td>
<td> <input type="password" name="password" maxlength="50"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="login" value="Login"></td>
</tr>
</table>
</form>
Subscribe to:
Comments (Atom)