Ini adalah source code untuk membuat login dengan menggunakan bahasa pemrograman PHP dan menggunakan Database MySQL
addauser.php
<?php// from: http://php.thedemosite.co.uk/
// version 1.2$page_title="Add a user - FREE PHP code and SQL";$page_keywords="free php, sql insert record";$page_desc="Add a username and password to test - free online login PHP MySQL example system";
include("header.inc");?>
<?php include("validateform.inc"); ?><p align="center"><big><strong>3. Add a User</strong></p></big>
<blockquote>
<blockquote>
<p>Below is the current single record within the database: </p>
</blockquote>
<blockquote>
<blockquote>
<?php include_once("config.php");
include_once("clean_input.php");
include_once("dbc.php");
include("getrecord.inc"); ?> </blockquote>
</blockquote>
<blockquote>
<p>Add your own username and password, enter your details below and click save. <strong>DO
NOT</strong> use valid details as the information you enter will be displayed above and
the next visitor will be able to view them as you are now.</p>
</blockquote>
</blockquote>
<form
ACTION="savedata.php"
name="saveform" METHOD="POST" align="center">
<div align="center"><center><table border="0" width="93%" cellspacing="0" cellpadding="0">
<tr>
<td width="52%"><div align="center"><center><table border="0" height="59" width="310"
bgcolor="#808080" cellspacing="1" cellpadding="0">
<tr>
<td width="248" height="19" bgcolor="#C0C0C0" align="right"><p><font color="#000000"><small>Add
Your test username:</small></font></td>
<td width="123" height="19" bgcolor="#C0C0C0"><p><input NAME="username"
VALUE SIZE="8" MAXLENGTH="16" tabindex="1"></td>
<td width="47" height="19" align="center" bgcolor="#C0C0C0"><div align="center"><center><p><a
href="javascript:alert('The username must be between 4 and 16 characters long.')"><small><small>Help</small></small></a></td>
</tr>
<tr align="center">
<td width="248" height="17" bgcolor="#C0C0C0" align="right"><p><font color="#000000"><small>Add
Your test password:</small></font></td>
<td height="17" width="123" bgcolor="#C0C0C0" align="left"><p><input type="password"
name="password" size="8" tabindex="2" maxlength="8"></td>
<td width="47" height="17" align="center" bgcolor="#C0C0C0"><a
href="javascript:alert('The password must be between 4 and 8 characters long.')"><small><small>Help</small></small></a></td>
</tr>
<tr align="center">
<td width="248" height="1" bgcolor="#C0C0C0"></td>
<td width="123" height="1" bgcolor="#C0C0C0"><p><input TYPE="button"
NAME="FormsButton2" VALUE="save" ONCLICK="validateForm()" tabindex="3"></td>
<td width="47" height="1" align="center" bgcolor="#C0C0C0"><a
href="javascript:alert('Click to save the details')"><small><small>Help</small></small></a></td>
</tr>
</table>
</center></div></td>
<td width="48%" align="center"><small>When you have added your own username and password <a
href="login.php">move
onto the Login page to test it!</a></small></td>
</tr>
</table>
</center></div>
</form>
<p align="center"> </p>
<p align="center"><a
href="addausercode.php">Click
here to view the PHP and JavaScript code</a> used for this page, or <a href="demo-code.zip">download the free zip here</a> </p>
<?php include("footer.inc"); ?>
validateform.inc
<noscript>
<p><strong><font color="#FF0000"><big>Warning: </big>Your Internet Browser has JavaScript
switched off or is an older browser. You will not be able to complete this
form. Please switch on JavaScript or return with a newer browser. </font></strong></p>
</noscript>
<script LANGUAGE="javaScript">
<!--
function validateForm()
{
with (document.saveform)
{
var saveit = true;
if (username.value.length < 4)
{
alert ("Username too short. The username must be at least 4 characters in length.");
username.focus();
username.select();
saveit = false;
}
if ((password.value.length < 4) && saveit)
{
alert ("Password too short. The password must be at least 4 characters in length.");
password.focus();
password.select();
saveit = false;
}
if ((fswords(username.value)) && saveit)
{
alert ("The username contains swear words. Please change it you silly person!");
username.focus();
username.select();
saveit = false;
}
if ((fswords(password.value)) && saveit)
{
alert ("The password contains swear words. Please change it you silly person!");
password.focus();
password.select();
saveit = false;
}
else if (saveit) submit();
}
}
swords = new Array()
swords [0] = "fuck"
swords [1] = "shit"
swords [2] = "bastard"
swords [3] = "wank"
swords [4] = "arse"
swords [5] = "bitch"
swords [6] = "cunt"
function fswords(theword)
{
thereturn=false;
theword = theword.toLowerCase();
for (i=0; i < swords.length; i++)
{
testit=theword.indexOf(swords[i],0);
//alert(swords[i]+ " testit ="+testit)
if (testit > -1) thereturn=true;
}
return thereturn
}
//-->
</script>
config.php
<?php// from: http://php.thedemosite.co.uk/
// version 1.2define('DBHOST', 'localhost');// MySQL host address - localhost is usually finedefine('DBUSER', ''); // db usernamedefine('DBPASS', ''); // db passworddefine('DBNAME', ''); // db name
define('DEBUG', false);// set to false when done testing
define('CHARSET',"utf8"); // sets the charset of your database for communicationdefine('DBDRIVER', 'mysql');// database driver to usedefine('DBPORT', '3306'); // database port for connection
clean_input.php
<?php// from: http://php.thedemosite.co.uk/
// version 1.0 if (!function_exists('clean_input')) // check to see if function is not already defined by another application{
function clean_input($string)
{
$patterns = array(// strip out:
'@script*?>.*?</script@si', // javascript
'@<[\/\!]*?[^<>]*?>@si', // HTML tags
'@"@si', //double quotes
"@'@si" //single quotes
);
$string = preg_replace($patterns,'',$string);
$string = trim($string);
$string = stripslashes($string);
return htmlentities($string);
}
}
foreach ($_REQUEST AS $key => $value) $$key = clean_input($value);//clean any and all user input
dbc.php
<?php// from: http://php.thedemosite.co.uk/
// version 1.0class dbc extends PDO{
protected static $instance;
public function __construct()
{
$options = array(PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '".CHARSET."';" // this command will be executed during every connection to server - suggested by: vit.bares@gmail.com
);
try {
$this->dbconn = new PDO(DBDRIVER.":host=".DBHOST.";port=".DBPORT.";dbname=".DBNAME,DBUSER,DBPASS,$options);
return $this->dbconn;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function reportDBError($msg)
{
if (DEBUG) print_r('<div style="padding:10%;"><h3>'.nl2br($msg).'</h3>(debug ref. 3.9d)</div>');
else
{
if(!session_id()) session_start();
$_SESSION['mysql_errors'] = "\n\nDb error: ".$msg."\n";
}
}
public static function instance()
{
if (!isset(self::$instance)) self::$instance = new self();
return self::$instance;
}
public function prepare($query, $options = NULL) {
try { return $this->dbconn->prepare($query); }
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function bindParam($query) {
try { return $this->dbconn->bindParam($query); }
catch (PDOException $e){ $this->reportDBError($e->getMessage()); }
}
public function query($query) {
try {
if ($this->query($query)) return $this->fetchAll();
else return 0;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); } }
public function execute($result) {//use for insert/update/delete
try { if ($result->execute()) return $result; }
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); }
}
public function executeGetRows($result) {//use to retrieve rows of data
try {
if ($result->execute()) return $result->fetchAll(PDO::FETCH_ASSOC);
else return 0;
}
catch (PDOException $e){ $this->reportDBError($e->getMessage()."<hr>".$e->getTraceAsString()); }
}
public function __clone()
{ //not allowed
}
public function __destruct()
{
$this->dbconn = null;
}
}
getrecord.inc
<?php// from: http://php.thedemosite.co.uk/
// version 1.2$dbc = dbc::instance();$result = $dbc->prepare("SELECT * from members where id = '1' ");$rows = $dbc->executeGetRows($result);
if(count($rows))
{
foreach ($rows[0] AS $key => $value) $$key = $value;
echo "<b>The username:</b> $username<br>";
echo "<b>The password:</b> $password<br>";
}
else echo "ERROR - unable to find current username and password!";
Comments
Post a Comment
terima kasih telah berpartisipasi pada blog kami