在php中我们要登录一般我们都是结合mysql session这两个结合实现的,下面我举一个例子来介绍php+mysql+session登录实例程序代码,有需要学习的朋友可参考.
实例代码如下
- ?<?php
-
-
- function safestrip($string){
- $string = strip_tags($string);
- $string = mysql_real_escape_string($string);
- return $string;
- }
-
-
- function messages() {
- $message = '';
- if($_SESSION['success'] != '') {
- $message = '<span id="message">'
- .$_SESSION['success'].'</span>';
- $_SESSION['success'] = '';
- }
- if($_SESSION['error'] != '') {
- $message = '<span id="message">'
- .$_SESSION['error'].'</span>';
- $_SESSION['error'] = '';
- }
- return $message;
- }
-
-
- function login($username, $password){
-
-
- $user = safestrip($username);
- $pass = safestrip($password);
-
-
- $pass = md5($pass);
-
-
- $sql =
- mysql_query("SELECT * FROM user_table WHERE username = '$user'
- AND password = '$pass'")or die(mysql_error());
-
-
- if (mysql_num_rows($sql) == 1) {
-
-
- $_SESSION['authorized'] = true;
-
-
- $_SESSION['success'] = '登录成功';
- header('Location: ./index.php');
- exit;
-
- } else {
-
- $_SESSION['error'] = '非常抱歉,您输入的用户名或密码有误';
- }
- }
- ?>
原理很简单,用户提交用户名与密码然后我们经过安全处理,然后再到mysql去精确比较如果相同就登录成功了. |