本文目錄一覽:
- 1、求php實現登錄成功後返回首頁並在原登錄處顯示用戶名的源代碼(類似於鐵血網的登錄)
- 2、跪求簡單的php用戶註冊源碼
- 3、求一簡單的PHP登錄源碼
- 4、請編一個PHP的登錄界面
- 5、PHP實現一個賬號同一時間只能一人登陸,給出源代碼!
求php實現登錄成功後返回首頁並在原登錄處顯示用戶名的源代碼(類似於鐵血網的登錄)
?php
session_start(); //用SESSION記錄模式,並開啟。
//用戶登錄信息,並記錄
if(@$_POST[‘GoLog’]){
$_SESSION[‘user’] = @$_POST[‘login’];
}
//主頁的檢查用戶登錄與否,並顯示歡迎信息
if(@$_SESSION[‘user’] == ”){
echo “目前沒有登錄”;
}else{
echo $_SESSION[‘user’].”歡迎你的回來!”;
}
?
form action=” method=’post’
input type=’text’ name=’login’ /
input type=’submit’ name=’GoLog’ value=’登錄’ /
/form
我只是給你寫了個原理,你可以加入更多的判斷。
跪求簡單的php用戶註冊源碼
?php
include ‘conn.php’;
if($_POST[‘submit’]){
$sql = “INSERT INTO admin (Aid,Aname,Apass) values (”,’$_POST[Aname]’,’$_POST[Apass]’)”;
mysql_query($sql);
echo $sql;
}
?
script type=”text/javascript”
function regcheck(){
if(regform.Aname.value==””){
alert(“用戶名不得為空”);
regform.Aname.focus();
return false;
}
if(regform.Apass.value==””){
alert(“密碼不得為空”);
regform.Apass.focus();
return false;
}
}
/script
form action=”zhuceyeA.php” name=”regform” method=”post” onsubmit=”return regcheck()”
註冊管理員br
用戶名:input type=”text” name=”Aname”
密碼:input type=”password” name=”Apass”
input type=”submit” name=”submit” value=”註冊”
/form
//我也是初學者 大四 這個代碼是我自己練手做的 資料庫很簡單 ID 用戶名 密碼(明文顯示,為了簡便起見) 高手不要笑話我。。。。
求一簡單的PHP登錄源碼
夠簡單吧。。密碼自帶。。不用連接資料庫。。只是登錄而已。
要是連接資料庫無非就是加一個連接和查詢語句。。然後判斷if正確不
?php
$admin[‘check’] = “1”;
$admin[‘pass’] = “1234567”;
$hidden = “saiy”;
$admin[‘port’] = “80,”;
$self = $_SERVER[‘PHP_SELF’];
if($admin[‘check’] == “1”) {
if ($_GET[‘action’] == “logout”) {
setcookie (“adminpass”, “”);echo “meta http-equiv=\”refresh\” content=\”0;URL=”.$self.”\””;
exit;
}
if ($_POST[‘do’] == ‘login’) {
$thepass=trim($_POST[‘adminpass’]);
if ($admin[‘pass’] == $thepass) {
setcookie (“adminpass”,$thepass,time()+(1*24*3600));echo “meta http-equiv=\”refresh\” content=\”0;URL=”.$self.”\””;
exit;
}
}
if (isset($_COOKIE[‘adminpass’])) {
if ($_COOKIE[‘adminpass’] != $admin[‘pass’]) {
loginpage();
}
}
else {
loginpage();
}
}
?
這裡是網頁html代碼
?php
/*======================================================
函數
======================================================*/
// 登陸入口
function loginpage() {
global $hidden;
if($hidden==”saiy”){
?
!– 登陸入口–
style type=”text/css”
input {font-family: “Verdana”;font-size: “11px”;BACKGROUND-COLOR: “#FFFFFF”;height: “18px”;border: “1px solid #666666”;}
a{text-decoration:none}
/style
table width=”416″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″
form method=”POST” action=””
tr
td height=”30″ align=”center”/td
/tr
tr
td height=”30″ align=”center”歡迎使用。。請先登陸!/td
/tr
tr
td height=”30″ align=”center”
span style=”font-size: 11px; font-family: Verdana”Password: /spaninput name=”adminpass” type=”password” size=”20″
input type=”hidden” name=”do” value=”login”
input type=”submit” value=”Login”
/td
/tr
/form
form
?php
{
?
/table
div align=”right” style=”color:#FFFFFF”a href=”del.php” style=”color:#FFFFFF”Powered By cfly/a/div
?}
}?
?php
exit;
}//end loginpage()
?
請編一個PHP的登錄界面
?php
if(isset($_GET[‘xname’]) isset($_GET[‘pass’]))
{
$user=$_GET[‘xname’];
$pass=$_GET[‘pass’];
if($user!=”” $pass!=””) //如果用戶名密碼不為空
{
$conn=mysql_connect(“localhost”,”root”,”123″);
mysql_select_db(“abc”,$conn);
$sql=sprintf(“select * from cba where user=’%s’ and pass=’%s'”,$user,$pass);
$rs=mysql_query($sql) or die(mysql_error());
$rows=mysql_num_rows($rs);
if($rows0) //登錄成功
{
echo ‘The Login Is OK, ‘;
echo ‘Please Enter Your ID.’;
exit;
}
}
}
echo “-3”;
?
PHP實現一個賬號同一時間只能一人登陸,給出源代碼!
對於一個帳號在同一時間只能一個人登錄,可以通過下面的方法實現:
1 .在用戶登錄時,把用戶添加到一個ArrayList中
2 .再次登錄時查看ArrayList中有沒有該用戶,如果ArrayList中已經存在該用戶,則阻止其登錄
3 .當用戶退出時,需要從該ArrayList中刪除該用戶,這又分為三種情況
① 使用註銷按鈕正常退出
② 點擊瀏覽器關閉按鈕或者用Alt+F4退出,可以用javascript捕捉該頁面關閉事件,
執行一段java方法刪除ArrayList中的用戶
③ 非正常退出,比如客戶端系統崩潰或突然死機,可以採用隔一段時間session沒活動就刪除該session所對應的用戶來解決,這樣用戶需要等待一段時間之後就可以正常登錄。
在LoginAction中定義:
// 用來在伺服器端存儲登錄的所有帳號
public static List logonAccounts;
login() 登錄方法中:
// 設置session不活動時間為30分
request.getSession().setMaxInactiveInterval(60*30);
if(logonAccounts==null){
logonAccounts = new ArrayList();
}
// 查看ArrayList中有沒有該用戶
for (int i = 0; i logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
return “denied”;
}
}
// 在用戶登錄時,把sessionId添加到一個account對象中
// 在後面 ③ 需要根據此sessionId刪除相應用戶
account.setSessionId(request.getSession().getId());
// 該用戶保存到ArrayList靜態類變數中
logonAccounts.add(account);
return “login”;
① 使用註銷按鈕正常退出
logout() 退出方法中:
if(logonAccounts==null){
logonAccounts = new ArrayList();
}
// 刪除ArrayList中的用戶 ⑴
for (int i = 0; i logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
logonAccounts.remove(account);
}
}
② 點擊瀏覽器關閉按鈕或者用Alt+F4退出:
在後台彈出一個窗口,在彈出窗口中刪除ArrayList中的用戶
function window.onbeforeunload(){
// 是否通過關閉按鈕或者用Alt+F4退出
// 如果為刷新觸發onbeforeunload事件,下面if語句不執行
if (event.clientXdocument.body.clientWidth event.clientY0||event.altKey){
window.open(‘accountUnbound.jsp’,”,
‘height=0,width=0,top=10000,left=10000’)
}
}
accountUnbound.jsp : 彈出窗口中刪除ArrayList中的用戶
%
Account account = (Account) request.getSession().getAttribute(“account”);
if(account != null){
if(LoginAction.logonAccounts==null){
LoginAction.logonAccounts = new ArrayList();
}
// 刪除ArrayList中的用戶——下面代碼和上面的 ⑴ 處一樣
for (int i = 0; i logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getAccountId().equals(existAccount.getAccountId())){
logonAccounts.remove(account);
}
}
}
%
為了保證上面代碼可以執行完畢,3秒後關閉此彈出窗口(也位於accountUnbound.jsp中)
script
setTimeout(“closeWindow();”,3000);
function closeWindow(){
window.close();
}
/script
③ 使LoginAction 實現implements HttpSessionListener,並實現sessionCreated,sessionDestroyed方法,在sessionDestroyed中刪除ArrayList中的用戶(用戶超過30分鐘不活動則執行此方法)
public void sessionDestroyed(HttpSessionEvent event) {
// 取得不活動時的sessionId,並根據其刪除相應logonAccounts中的用戶
String sessionId = event.getSession().getId();
for (int i = 0; i logonAccounts.size(); i++) {
Account existAccount = (Account)logonAccounts.get(i);
if(account.getSessionId().equals(existAccount.getSessionId())){
logonAccounts.remove(account);
}
}
}
註:
對於上面的,由於彈出窗口很容易被防火牆或者安全軟體阻攔,造成無法彈出窗口,從而短時間不能登錄,這種情況可以用AJAX來代替彈出窗口,同樣在後台執行刪除用戶的那段代碼,卻不會受到防火牆限制:
script
// ![CDATA[
var http_request = false;
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,…
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType(‘text/xml’);
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
http_request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e) {
}
}
}
if (!http_request) {
alert(‘Giving up :( Cannot create an XMLHTTP instance’);
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open(‘GET’, url, true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
window.close();
} else {
alert(‘There was a problem with the request.’);
}
}
}
function window. onbeforeunload() {
makeRequest (‘accountUnbound.jsp’);
}
//]]
/script
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/290755.html