php用静态类实现的页面跳转,有需要的朋友,可以参考学习下。
1、页面跳转
<?php
/**
* Miscellaneous utility methods.
*/
final class Utils {
private function __construct() {
}
/**
* Redirect to the given page.
* @param type $page target page
* @param array $params page parameters
*/
public static function redirect($page, $sub_page = null, array $params = array()) {
header('Location: ' . self::createLink($page, $sub_page, $params));
die();
}
/**
* Generate link.
* @param string $page target page
* @param array $params page parameters
*/
public static function createLink($page, $sub_page = null, array $params = array()) {
if ($sub_page) {
$params = array_merge(array('sub_page' => $sub_page), $params);
}
if (!$page) {
return "#";
}
$param_str = http_build_query($params);
if (trim($param_str) == "") {
return $page . '.php';
} else {
return $page . '.php?' . $param_str;
}
}
}
?>
调用示例:
if (UserLogin::isLogin() && $_COOKIE["user_id"]==1) {
UserLogin::delUserInfo();
}
else if (UserLogin::isLogin()){
Utils::redirect('welcome');
}
?>
1、用户登录状态操作类UserLogin
<?php
final class UserLogin {
public function __construct() {
}
public static function getUserInfo() {
if (isset()($_COOKIE["user_id"])&&$_COOKIE["user_id"]&&(trim($_COOKIE["user_id"])!="")) {
if (isset($_SESSION["USER_INFO"]))
return $_SESSION["USER_INFO"];
$dao = new UserDao();
$user = $dao->find($_COOKIE["user_id"]);
if ($user) {
$_SESSION["USER_INFO"] = $user;
setcookie("docloud_sid", session_id(), time() + 36000);
setcookie("user_id", $_COOKIE["user_id"], time() + 36000);
if (array_key_exists("selected_prj_id", $_COOKIE))
setcookie("selected_prj_id", $_COOKIE["selected_prj_id"], time() + 36000);
if (array_key_exists("selected_class_id", $_COOKIE))
setcookie("selected_class_id", $_COOKIE["selected_class_id"], time() + 36000);
if (array_key_exists("selected_image_id", $_COOKIE))
setcookie("selected_image_id", $_COOKIE["selected_image_id"], time() + 36000);
if (array_key_exists("test_image_ids", $_COOKIE))
setcookie("test_image_ids", $_COOKIE["test_image_ids"], time() + 36000);
if (array_key_exists("upload_image_ids", $_COOKIE))
setcookie("upload_image_ids", $_COOKIE["upload_image_ids"], time() + 36000);
return $user;
}
}
self::clearCookie();
return null;
}
public static function setUserInfo($userInfo) {
$_SESSION["USER_INFO"] = $userInfo;
setcookie("docloud_sid", session_id(), time() + 36000);
setcookie("user_id", $userInfo->getId(), time() + 36000);
}
public static function isLogin() {
if (self::getUserInfo()) {
return true;
}
return false;
}
public static function delUserInfo() {
self::clearCookie();
session_destroy();
}
private static function clearCookie() {
setcookie("docloud_sid", "", time() - 36000);
setcookie("user_id", "", time() - 36000);
setcookie("selected_prj_id", "", time() - 36000);
setcookie("selected_class_id", "", time() - 36000);
setcookie("selected_image_id", "", time() - 36000);
setcookie("test_image_ids", "", time() - 36000);
setcookie("upload_image_ids", "", time() - 36000);
}
}
?>
2、在用户输入用户名、密码处调用来做相关判定
<?php
require_once 'Init.php';
// if logged in, logout
if (UserLogin::isLogin() && $_COOKIE["user_id"]==1) {
UserLogin::delUserInfo();
}
else if (UserLogin::isLogin()){
Utils::redirect('welcome');
}
$username = null;
$password = null;
$msg = "";
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = addslashes(trim(stripslashes()($_POST ['username'])));
$password = addslashes()(trim(stripslashes($_POST ['password'])));
// validate
$errors = LoginValidator::validate($username, $password);
if (empty($errors)) {
// save
$dao = new UserDao();
$user = $dao->findByName($username);
$last_login_ip = Utils::getIpAddress();
$user->setLastLoginIp($last_login_ip);
$now = new DateTime();
$user->setLastLoginTime($now);
$dao->save($user);
UserLogin::setUserInfo($user);
Flash::addFlash('登录成功!');
Utils::redirect('welcome');
}
foreach ($errors as $e) {
$msg .= $e->getMessage()."<br>";
}
}
?>
一个php的mysql操作类,简单实用,供大家参考。
<?php
//数据库操作类
class db
{
//SQL执行后的数据保存变量;
var $db;
//读取或设置当前数据的位置
var $position=0;
//执行SQL语句并把结果保存为db变量中;
function sub_sql($str)
{
global $prefix;//全局函数,表前缀
return str_replace()("#@__",$prefix,$str);
}
function Sql($str)
{
$str=$this->sub_sql($str);
$result = mysql_query()($str);
$i=0;
while($row = mysql_fetch_array($result))
{
$str_array[$i]=$row;
$i++;
}
if(empty($str_array))
{
$str_array=array();
}
$this->db=$str_array;
}
//读取一条数据并把数据往后移一位,如果数据为空则返回为null;
function Get_One()
{
$re=empty($this->db[$this->position])?null:$this->db[$this->position];
$this->position=$re?$this->position+1:$this->position;
return $re;
}
//判断是否数据读取到结尾了
function Judge()
{
$re=empty($this->db[$this->position])?true:false;
return $re;
}
//取得db里面的个数
function Get_Num()
{
return count($this->db);
}
//更新数据库里面的数据,$t为表名,$v格式为数组格式,上标为字段名,下标为数据;$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
function Set_Updata($t,$v,$w,$p=0)
{
$this->Sql($t);
$v_str="";
$w_str="";
$f="";
foreach($v as $key=>$vaule)
{
if(!is_numeric($key))
{
if(empty($v_str))
{
$v_str=htmlspecialchars()($key)."='".htmlspecialchars($vaule)."'";
}else
{
$v_str=$v_str.",".htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
}
}
}
switch($p)
{
case 0:
$f="=";
break;
case 1:
$f=">";
break;
case -1:
$f="<";
break;
}
if(!empty($f))
{
foreach($w as $key=>$vaule)
{
if(!is_numeric($key))
{
if(empty($v_str))
{
$w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}else
{
$w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}
}
}
}
$sql="UPDATE ".$t." SET ".$v_str." where ".$w_str;
return $result = mysql_query($sql);
}
//删除一数据$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
function Set_Del($t,$w,$p=0)
{
$this->sub_sql($t);
$w_str="";
$f="";
switch($p)
{
case 0:
$f="=";
break;
case 1:
$f=">";
break;
case -1:
$f="<";
break;
}
if(!empty($f))
{
foreach($w as $key=>$vaule)
{
if(!is_numeric($key))
{
if(empty($v_str))
{
$w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}else
{
$w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}
}
}
}
$str="DELETE FROM ".$t." WHERE ".$w_str;
return $result = mysql_query($str);
}
function Add($t,$v)
{
$this->sub_sql($t);
$k_str="";
$v_str="";
foreach($v as $key=>$vaule)
{
if(!is_numeric($key)){
if(empty($k_str))
{
$k_str=htmlspecialchars($key);
$v_str="'".htmlspecialchars($vaule)."'";
}else
{
$k_str=$k_str.",".htmlspecialchars($key);
$v_str=$v_str.","."'".htmlspecialchars($vaule)."'";
}
}
}
$str="INSERT INTO ".$t."(".$k_str.")"."value(".$v_str.")";
return $result = mysql_query($str);
}
}
?>