09/10/2018, 17:53
[Q] Xin soruce login PHP
Bác nào có source login PHP đơn giản share cho mình với. Mình có 2 cái nhưng cookie kỳ lắm, login một lần thì lần sau vô được hoài.
Không rành PHP lắm, mong các bác cho source đầy đủ. Thanks :o
Không rành PHP lắm, mong các bác cho source đầy đủ. Thanks :o
Bài liên quan





1. Neu muon login thi setcokie cho no
2. Neu muon logout thi setcookie la cac chuoi rong
3. Viet mot module khi include thi truoc tien la doc cookie de test user, neu thanh cong => Test Autolog => Lam gi nua do thi tuy . Neu lan duoc da logout, thi lan sau lam gi con User va Pass de test nua
http://www.diendantinhoc.com/showthr...threadid=13834
Chào.
LQ
Cám ơn rất nhiều !!!
Không rành PHP lắm, mong các bác cho source đầy đủ. Thanks ***
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Có cho bạn đây. Chúc thành công .Thân KP
<?
function MakeTableLogins($database, $host, $db_user, $db_pass) {//create the logins table
$linkID = mysql_connect($host, $db_user, $db_pass);
mysql_select_db($database, $linkID);
mysql_query("create table logins (user char(32), pasword char(32))", $linkID);
}
function Encrypt($string) {//hash then encrypt a string
$crypted = crypt(md5($string), md5($string));
return $crypted;
}
function AddUser($database, $host, $db_user, $db_pass, $username, $password) { //add user to table logins
$linkID = mysql_connect($host, $db_user, $db_pass);
mysql_select_db($database, $linkID);
$password = encrypt($password);
$username = encrypt($username);
mysql_query("insert into logins values ('$username', '$password')", $linkID);
}
function Login($database, $host, $db_user, $db_pass, $user, $password) { //attempt to login false if invalid true if correct
$auth = false;
$user = Encrypt($user);
$linkID = mysql_connect($host, $db_user, $db_pass);
mysql_select_db("$database", $linkID);
$result = mysql_query("select password from logins where user = '$user'", $linkID);
$pass = mysql_fetch_row($result);
mysql_close($linkID);
if ($pass[0] === (Encrypt($password))) {
$auth = true;
}
return $auth;
}
?>
Đầu tiên tạo một Rooth Path bằng php ! đặt nó là : root_path.php
<?
/*
// --: http://www.tuyen4ever.org
*/
$root_path = "/data/members/paid/t/u/tuyen4ever.org/htdocs/www/forums/"; // Forums root path, you can get this from your AdminCP. MAKE SURE YOU PUT THE FULL PATH LIKE THE EXAMPLE.
$skin_path = "/data/members/paid/t/u/tuyen4ever.org/htdocs/www/skin/";// Portal Skins root path. MAKE SURE YOU PUT THE FULL PATH LIKE THE EXAMPLE.
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cái này phải chỉnh nhá ! chỉnh cái Path cho phù hợp nhá
Còn đây là file login.php
<?php
/*
// -- : http://www.tuyen4ever.org
*/
error_reporting(E_ALL);
require("root_path.php");
//$root_path = "c:/e drive/ibf/"; // Set this to the path of the forums
require( $root_path . "conf_global.php" );
$connect = mysql_connect( $INFO['sql_host'] , $INFO['sql_user'] , $INFO['sql_pass'] );
mysql_select_db( $INFO['sql_database'] , $connect ) or die( "Could not select the DB" );
//---------- Logs in the user ----------//
// Checking if the user has filled in the form or not
// If yes then assigning it to variables
if( isset( $HTTP_POST_VARS['UserName'] ) && isset( $HTTP_POST_VARS['PassWord'] ) )
{
$user = $HTTP_POST_VARS['UserName'];
$pass_md5 = md5( $HTTP_POST_VARS['PassWord'] );
}
// If no then showing an error
else
{
echo "One of the form fields was not completed. That is either the username or password was not filled in";
exit;
}
// Running a query to get the user info
$query_l = mysql_query( "SELECT * FROM ".$INFO['sql_tbl_prefix']."members WHERE name='$user' && password='$pass_md5'" );
// If user info provided is correct then logging in the user
if( mysql_num_rows( $query_l ) > 0 )
{
// Creating session id
$session_id = md5( uniqid( microtime() ) );
$arr = mysql_fetch_array( $query_l );
$id = $arr['id'];
$browser = substr($HTTP_SERVER_VARS['HTTP_USER_AGENT'], 0, 50);
$ip = substr($HTTP_SERVER_VARS['REMOTE_ADDR'], 0, 50);
// Deleting any old cookies
mysql_query( "DELETE FROM ibf_sessions WHERE ip_address='".$HTTP_SERVER_VARS['REMOTE_ADDR']."'" );
// Updating sessions table for IBF to recognize the user
mysql_query( "INSERT INTO ibf_sessions (id,member_name,member_pass,member_id,running_time ,member_group,"
."ip_address,browser,start_session,login_type) VALUES ('$session_id','".$arr['name']."','".$arr['password']."','"
.$arr['id']."','".time()."','".$arr['mgroup']."','$ip','$browser','".time()."',"
."'0')" );
// Setting cookies for IBF to recognize the user
setcookie( $INFO['cookie_id']."member_id" , $id , time()+7200 , $INFO['cookie_path'] , $INFO['cookie_domain'] );
setcookie( $INFO['cookie_id']."pass_hash" , $pass_md5 , time()+7200 , $INFO['cookie_path'] , $INFO['cookie_domain'] );
setcookie( $INFO['cookie_id']."anonlogin" , "-1" , time()+7200 , $INFO['cookie_path'] , $INFO['cookie_domain'] );
setcookie( $INFO['cookie_id']."session_id" , $session_id , time()+7200, "/" , $INFO['cookie_domain'] );
// Redirecting the user to the page he came from
header( "Location: ".$HTTP_SERVER_VARS['HTTP_REFERER'] );
}
// If user info provided is wrong then error is displayed
else
{
echo "Either login name or the password is wrong.";
exit;
}
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nhớ phải chỉnh đương Path của cái này cho nó đến Forrums
Sau đây là đoạn code html skin ! đặt nó là : skin_login.html
<script language='JavaScript'>
<!--
function ValidateForm() {
var Check = 0;
if (document.LOGIN.UserName.value == '') { Check = 1; }
if (document.LOGIN.PassWord.value == '') { Check = 1; }
if (Check == 1) {
alert('Please enter your name and password before continuing');
return false;
} else {
document.LOGIN.submit.disabled = true;
return true;
}
}
//-->
</script>
<form action='../login.php' method='post' name='LOGIN' onSubmit='return ValidateForm()'>
<input type='hidden' name='act' value='Login'>
<table cellpadding='1' cellspacing='1' border='0' width='100%' bgcolor='#FFFFFF'>
<tr>
<td align='left'bgcolor='#FFFFFF'>
<table style='border:0px dotted gray;width:100%;'>
<tr><td nowrap><b>User Name:</b></td><td><input type='text' name='UserName' maxlength='64' style='width:120px;height=20px;font-family:arial;font-size:8pt'></td></tr>
<tr><td><b>Password:</b></td><td><input type='password' name='PassWord'style='width:120px;height=20px;font-family:arial;font-size:8pt'></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Login'></td></tr>
</table>
</td>
</tr>
</table>
</form>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thân KP (tuyen4ever)