php,ajax登陆退出
利用ajax可以做到页面无刷新登陆。
运行效果
目录结构
site/
css/
images/
js/
site/css/bootstrap.css(bootstrap样式表)
site/js/bootstrap.js(bootstrap脚本)
site/js/jquery-2.1.0.js(jQuery)
site/images/ajax-loader.gif
site/index.php
<?php
session_start();
?>
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ajax,php登陆</title>
<!--bootstrap样式表-->
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/main.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container">
<?php
if(!isset($_SESSION['username'])){
echo '
<form method="post" class="form-signin">
<h2 class="form-signin-heading">登陆</h2>
<input type="text" placeholder="用户名" class="form-control" id="username" />
<input type="password" placeholder="密码" class="form-control" id="password" />
<button id="submit" type="submit" class="btn btn-lg btn-primary btn-block">登陆</button>
<div id="message"></div>
</form>
';
} else {
echo '
<div class="form-signin">
<div class="alert alert-success">登陆<strong>成功</strong>。</div>
<a href="logout.php" class="btn btn-default btn-lg btn-block">退出登陆</a>
</div>
';
}
?>
</div>
</body>
<!--jQuery-->
<script src="js/jquery-2.1.0.js"></script>
<!--booststrap库,一些方便的组件-->
<script src="js/bootstrap.js"></script>
<!--AJAX登陆脚本-->
<script src="js/login.js"></script>
</html>
site/css/main.css
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
} .form-signin {
max-width: 330px;
padding: 15px;
margin: auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
font-size: 16px;
height: auto;
padding: 10px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.form-signin .form-control:focus {
z-index: ;
}
.form-signin input[type="text"] {
margin-bottom: -1px;
border-bottom-left-radius: ;
border-bottom-right-radius: ;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: ;
border-top-right-radius: ;
}
.form-signin .btn {
margin-bottom: 10px;
}
site/js/login.js
$('#submit').click(function(e){
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
type:"POST",
url: "checklogin.php",
data: "myusername="+username+"&mypassword="+password,
success: function(html){
if(html=='true') {
window.location="index.php";
}
else {
$("#message").html(html);
}
},
beforeSend:function()
{
$("#message").html("<p class='text-center'><img src='images/ajax-loader.gif'></p>")
}
})
return false;
})
site/checklogin.php
<?php // 会话开始
session_start();
include_once 'config.php';
// 连接数据库
$mysqli = mysqli_connect($host, $username, $password, $db_name) or die("数据库链接失败"); // 获取用户名密码
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword']; // 防MySQL注入
$myusername = mysqli_real_escape_string($mysqli,$myusername);
$mypassword = mysqli_real_escape_string($mysqli,$mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result= mysqli_query( $mysqli,$sql); // Mysql_num_row 获取结果数
$count=mysqli_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){ // 打印 "true",并且将账号密码注册到session
echo "true";
$_SESSION['username'] = 'myusername';
$_SESSION['password'] = 'mypassword'; }
else {
// 返回错误信息,×为X号
echo "<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>账号密码错误</div>";
}
?>
site/logout.php
<?php
// 销毁session
session_start();
session_destroy();
header("location:index.php");
?>
site/config.php
<?php
$host="localhost"; // Host
$username="root"; // Mysql用户名
$password="12345"; // Mysql密码
$db_name="test"; // 数据库名
$tbl_name="members"; // 表名
?>
php,ajax登陆退出的更多相关文章
- iframe 的使用和登陆退出的实现——整个页面跳转
iframe中如果只是页面跳转的话,我们依然只是部分的加载的了,为了实现整个页面的所有内容跳转,下面提供了整个页面跳转的方法. iframe例子 1.总的iframe页面(访问就访问这个) all. ...
- 基于ThinkPHP3.23的简单ajax登陆案例
本文将给小伙伴们做一个基于ThinkPHP3.2.的简单ajax登陆demo.闲话不多说.直接进入正文吧. 可能有些小伙伴认为TP自带的跳转页面挺好,但是站在网站安全的角度来说,我们不应该让会员看到任 ...
- laravel前后端分离的用户登陆 退出 中间件的接口与session的使用
在项目开发的过程中,需要有用户的登陆 退出 还有校验用户是否登陆的中间件; 基本思路: 登陆: 前端请求接口的参数校验 用户名 密码规则的校验 用户名密码是否正确的校验; 如果上面的校验都通过的了,把 ...
- Ajax登陆,使用Spring Security缓存跳转到登陆前的链接
Spring Security缓存的应用之登陆后跳转到登录前源地址 什么意思? 用户访问网站,打开了一个链接:(origin url)起源链接 请求发送给服务器,服务器判断用户请求了受保护的资源. 由 ...
- Spring Security 使用Ajax登陆无法跳转页面解决方法
使用Security的朋友都知道,使用Security后,不再需要我们自己过多的(还需要写少量代码)写登陆的逻辑,只需要自己在html的登陆表单上面定义好输入框name为:username和passw ...
- Yii2 前后台登陆退出分离、登陆验证
这里用的yii2高级模板, 基本模板的配置文件在一个文件里,方法基本没什么区别, 1.用户表要有两个用户表, 当然一个也行,分开是省得麻烦,既然是分离了就彻底分开, 前台表user,后台表user_b ...
- netMVC 搭建Ucenter 同步登陆退出discuz
先看一下效果
- 用户登陆,退出等基本Action
用户登陆页面user_login.jsp对应action为login.do: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...
- IE浏览器下使用AJAX登陆接口请求缓存与登陆不了的问题解决
问题: 在IE浏览器下面,登陆的时候老是登陆不上,但是打开控制台的时候再登陆却能登陆上. 分析: 通过抓包,发现,在不打开控制台的时候,少了一个接口的请求,却返回了改接口的返回信息,但是返回信息并不是 ...
随机推荐
- asp.net基础学习笔记
原文地址:http://blog.csdn.net/oxoxzhu/article/details/8652530 1.概论 浏览器-服务器 B/S 浏览的 浏览器和服务器之间的交互,形成上 ...
- sql 2000 "无法执行查询,因为一些文件缺少或未注册"的
sql 2000 "无法执行查询,因为一些文件缺少或未注册"的解决办法 在SQL server 2000中打开表查看数据的时候,提示说“无法执行查询,因为一些文件缺少或未注册” 用 ...
- tty/pts 相关指令
http://unix.stackexchange.com/questions/136662/how-can-we-know-whos-at-the-other-end-of-a-pseudo-ter ...
- ios控制器modal跳转
1. http://www.cnblogs.com/smileEvday/archive/2012/05/29/presentModalViewController.html 2012年5月- Pre ...
- curl断点续载
摘自http://blog.csdn.net/zmy12007/article/details/37157297 摘自http://www.linuxidc.com/Linux/2014-10/107 ...
- Android的Activity切换动画特效库SwitchLayout,视图切换动画库,媲美IOS
由于看了IOS上面很多开发者开发的APP的视图界面切换动画体验非常好,这些都是IOS自带的,但是Android的Activity等视图切换动画并没有提供原生的,所以特此写了一个可以媲美IOS视图切换动 ...
- 很好很实用的.net、网站系统后台模板
本模板是程序园给大家提供的应用系统开发后台模板,主要使用div+css布局实现,菜单使用了ddaccordion.js菜单控件. 转载请标明:http://www.kwstu.com/ArticleV ...
- 【网络协议】TCP的拥塞控制机制
前言 计算机网络中的带宽.交换节点中的缓存和处理机等,都是网络的资源,在某段时间内,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就要变坏,这样的情况就叫做拥塞. 所谓拥塞控制,就 ...
- 每日一小练——Eratosthenes 筛选法
上得厅堂.下得厨房,写得代码.翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Eratosthenes筛选法 内容: 求质数是一个非常普遍的问题,通常不外乎用数去除.除到不尽时,给定的数就是质数.可是 ...
- OS Kernel Parameter.semopm
安装Oracle11g内核参数semopm未校验通过,点击Fix&Check Again后,会提示执行修改脚本,在/tmp/CVU_11.2.0.1.0_oracle下,找到并执行该脚本run ...