hehehe.. now the real stuff...
First you need to create a priviledge user for your database then you create your own database. the table that i used is test as you can find it in
$table that i declare in login.php you may put to field of username2 and password2.
connect.php
<?
/*
This is Database Connection
*/
$localhost = "localhost";
$userdb="test";
$passdb="test";
$database="test";
mysql_connect ($localhost, $userdb, $passdb) or die ('My SQL Error: ' . mysql_error());
mysql_select_db ($database);
?>
connect.php is a connection to you database as you can see there you need to create a user name and password also you database in you mysql. Example below:-
username = test
password = test
database name = test
mysql_connect ($localhost, $userdb, $passdb) or die ('My SQL Error: ' . mysql_error());
Here is a statement for you to connect to your database. In php we using
mysql_connect statement to connet to database.
('My SQL Error: ' . mysql_error() is an error statement that appear if the database that connected is available or not and also other error. This is the error handling.
login.php
<?
require_once "connect.php";
$table="test";
$username=$_POST['username2'];
$password=$_POST['password2'];
$stuff = mysql_query("SELECT * FROM $table WHERE username1='".$username."' AND password1='".$password."'") or die("MySQL Login Error: ".mysql_error());
if(isset($_POST['username2'],$_POST['password2']))
{
if (mysql_num_rows($stuff) > 0) {
$row=mysql_num_rows($stuff);
if(isset($_POST['remember']))
{
setcookie('username2', $_POST['username2'], time() + 31536000) or die('Could not set login cookie');
setcookie('password2', ($_POST['password2']), time() + 31536000) or die('Could not set login cookie');
}
else
{
setcookie('username2', $_POST['username2']) or die('Could not set login cookie');
setcookie('password2', ($_POST['password2'])) or die('Could not set login cookie');
}
echo "<p><form action=home.php><p align=center>Welcome <b>$username</b>.<br> Please press continue to continue.</p><p align=center><input class=button type=submit value=Continue></p></form>";
//echo "You are logged in as $username <br>";
}else{
echo'
<!-- ***** Below is all the HTML statement *****-->
<table align=center>
<tr>
<td align=center colspan=2>
<font size=10>Welcome</font>
</td>
</tr>
<tr>
<td colspan=2><font color=red>*</font>
Please Enter Username and Password</td>
</tr>
<form method=POST>
<tr>
<td>User name</td><td><input type=text name=username2></td>
</tr>
<tr>
<td>Password</td><td> <input type="password" name="password2" size="20" /></td>
</tr>
<tr>
<td align=center colspan=2><input type=submit value=LogIn>
<a href=signup.php>Sign Up</a>
</td>
</tr>
</form>
</table>
';}
}else {
echo"
<!-- ***** Below is all the HTML statement *****-->
<table align=center>
<tr>
<td align=center colspan=2>
<font size=10>Welcome</font>
</td>
</tr>
<form method=POST>
<tr>
<td>User name</td><td><input type=text name=username2></td>
</tr>
<tr>
<td>Password</td><td> <input type=password name=password2 size=20 /></td>
</tr>
<tr>
<td align=center colspan=2><input type=submit value=LogIn>
<a href=signup.php>Sign Up</a>
</td>
</tr>
</form>
</table>
";
}
?>
home.php
<?
ob_start();
//checking username and password from cookie
if(!isset($_COOKIE['username2'],$_COOKIE['password2']))
{ /*if there username2 and password2 not the same it will ask login first before enter*/
echo '<p align="center">Please <a href="login.php">login</a> first before Enter.</p>';}
else{
/*
After this echo'
put your own HTML code
*/
echo'
<p align=right>
<a href></a>
<a href=logout.php>Logout</a>
</p>
<div align="center">
<center>
<table border=1>
<tr>
<td colspan=2>test</td>
</tr>
<tr>
<td>test</td>
<td>test</td>
</tr>
</table>
</center>
</div>
';
}
?>
logout.php
<?php
/********************
* logout.php *
********************/
ob_start();
if(!isset($_COOKIE['username2'],$_COOKIE['password2']))
die('<form action="login.php"><p align="center">You are already logged out.<p align="center">Do you want to login?</p> <p align="center"><input class="button" type="submit" value="login"></p></form>');
setcookie('username2', $_COOKIE['username2'], time() - 3600);
setcookie('password2', $_COOKIE['password2'], time() - 3600);
echo '
<form action="login.php"><p align="center">You are already logged out.<p align="center">Do you want to login?</p> <p align="center"><input class="button" type="submit" value="login"></p></form>
';
?>
Well i think this is the simplest login and logout program that i've made well you guys can try and have fun also if you guys don't understand some of the statement just post it here....hope this will help freebies guys... to make their own web based programming....