码迷,mamicode.com
首页 > Web开发 > 详细

前端小白之每天学习记录----php(7)session与cookie

时间:2017-08-16 20:13:42      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:1.0   put   lang   添加   func   tco   web服务   location   zhang   

服务器端的会话技术(通讯协议)

坐地铁:
深圳通
拿手机刷

cookie( 保存在 客户端的浏览器里面的数据 )
session( 保存在 web服务器里面的数据 )

如何使用cookie?

1.设置/注销cookie(新建cookie.php)

<?php
/*
array( ‘user‘ => ‘ghostwu‘ )
*/
// function setCookie(c_name,value,expiredays)
                      // 变量名,值,过期天数
setcookie( "user", "zhangsan" );//设置成功
if( isset( $_GET[‘act‘] ) && $_GET[‘act‘] == ‘logout‘){
    //time(): 获取到当前的时间戳
    setcookie( "user", "", time() - 1 );//清空user的值,并使它过期
}

?>
  <a href="?act=logout">注销</a>

 2.取出cookie(新建cookie2.php)

<?php
    /*
        php里面的所有cookie都是存储在超级全局变量 $_COOKIE 里面
        $_COOKIE是储存在浏览器的类数组
    */
    // print_r( $_COOKIE );

    // echo $_COOKIE[‘user‘];
    if( !empty( $_COOKIE[‘user‘] ) ){
        echo $_COOKIE[‘user‘];
    }



?>

 

3.一个设置cookie的登录页面(新建login.php)

登录成功时候用cookie保存用户名与密码

setcookie( "name", $userName ); /

setcookie( "pwd", $userPwd );

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .reg-form {
      margin-top: 100px;
    }
  </style>
</head>

<body>
<!-- 链接数据库 -->
  <?php
require( ‘./mysql.class.php‘ );//链接数据库的类文件,请参考本系列上一节
?>

<!-- html文本 -->
    <div class="container">
      <div class="row">
        <div class="tip"></div>
        <form class="form-horizontal reg-form" role="form" method="post">
          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">用户名:</label>
            <div class="col-md-6">
              <input type="text" class="form-control" id="user" name="user" placeholder="请输入用户名">
            </div>
          </div>
          <div class="form-group">
            <label for="lastname" class="col-md-2 control-label">密码</label>
            <div class="col-md-6">
              <input type="password" name="pwd" class="form-control" id="pwd" placeholder="请输入密码">
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-primary">登录</button>
            </div>
          </div>
        </form>
      </div>
    </div>
<!-- PHP文本 -->
    <?php
if( !empty( $_POST[‘user‘] ) && !empty( $_POST[‘pwd‘] ) ){ //判断用户名密码非空
    $userName = $_POST[‘user‘];
    $userPwd = $_POST[‘pwd‘];
    $sql = "SELECT * FROM user_info WHERE user_name = ‘$userName‘ AND user_pwd = ‘$userPwd‘"; 
    $res = mysql_query( $sql );   //查询用户名密码
    if( $res && mysql_num_rows( $res ) ){ //如果查询成功,并且账号密码正确
        setcookie( "name", $userName ); //设置cookie
        setcookie( "pwd", $userPwd );
        header("Location:success.php"); //设置完跳转到success.php页面
?>
    <!-- <script>
        window.location.href = ‘./success.html‘;
    </script> -->
<?php
    }else {   //登录不成功,重新加载页面
        header("Location:login.php");
?>
    <!-- <script>
        window.location.href = ‘./error.html‘;
    </script> -->
<?php
    }
}
?>

</body>

</html>

  

 

4.使用保存的cookie --->登录成功页面(新建success.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <?php
        if( empty( $_COOKIE[‘name‘] ) ){    //是否有用户名
            header("Location:login.php");   // 没有用户名则跳到登录页面
        }

        //这里要添加一条判断 数据库查询用户名与密码是否正确
        //判断方法与登录页面相同,如果用户名密码错误,跳去登录页面

        //点击注销页面:使cookie过期
        if( isset( $_GET[‘act‘] ) && $_GET[‘act‘] == ‘logout‘ ){
            setcookie( ‘name‘, ‘‘, time() - 1 ); //注销cookie
            header("Location:login.php"); //注销后跳转到登录页面
        }
    ?>
   <h3>欢迎您
<!-- 把cookie保存的用户名取出来 -->
<?php echo !empty($_COOKIE[‘name‘]) ? $_COOKIE[‘name‘] : ‘‘; ?> 

!登录</h3> | 
<!-- 注销按钮 -->
<a href="?act=logout">注销</a>
</body>
</html>

如何使用session?

1.设置/注销session(新建session.php)

   <?php
    /*
        要使用session, 必须先要用session_start() 开启session
               web服务器上会创建一个类数组$_SESSION
    */

    session_start();

    $_SESSION[‘name‘] = ‘zhangsan‘;

    if( isset( $_GET[‘act‘] ) && $_GET[‘act‘] == ‘logout‘  ){
        //删除某个数组的一项,或者删除一个变量
        unset( $_SESSION[‘name‘] );
    }
?>


<a href="?act=logout">注销</a>

2.取出session(新建session2.php)

<?php
    session_start();
    if( !empty( $_SESSION[‘name‘] ) ){
        echo $_SESSION[‘name‘];
    }
?>

3.一个设置session的登录页面(新建login2.php)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .reg-form {
      margin-top: 100px;
    }
  </style>
</head>

<body>

  <?php
  session_start();
require( ‘./mysql.class.php‘ );
?>


    <div class="container">
      <div class="row">
        <div class="tip"></div>
        <form class="form-horizontal reg-form" role="form" method="post">
          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">用户名:</label>
            <div class="col-md-6">
              <input type="text" class="form-control" id="user" name="user" placeholder="请输入用户名">
            </div>
          </div>
          <div class="form-group">
            <label for="lastname" class="col-md-2 control-label">密码</label>
            <div class="col-md-6">
              <input type="password" name="pwd" class="form-control" id="pwd" placeholder="请输入密码">
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-primary">登录</button>
            </div>
          </div>
        </form>
      </div>
    </div>

<?php
if( !empty( $_POST[‘user‘] ) && !empty( $_POST[‘pwd‘] ) ){
    $userName = $_POST[‘user‘];
    $userPwd = $_POST[‘pwd‘];
    $sql = "SELECT * FROM user_info WHERE user_name = ‘$userName‘ AND user_pwd = ‘$userPwd‘"; 
    $res = mysql_query( $sql ); 
    if( $res && mysql_num_rows( $res ) ){
        $_SESSION[‘name‘] = $userName;
        $_SESSION[‘pwd‘] = $userPwd;
        header("Location:success2.php");
?>
    <!-- <script>
        window.location.href = ‘./success.html‘;
    </script> -->
<?php
    }else {
        header("Location:login2.php");
?>
    <!-- <script>
        window.location.href = ‘./error.html‘;
    </script> -->
<?php
    }
}
?>

</body>

</html>

4.使用保存的session --->登录成功页面(新建succeed2.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <?php
        session_start();    //开启session
        require("./mysql.class.php");//链接数据库的类文件,请参考本系列上一节
?>
<?php
    if( empty( $_SESSION[‘name‘] ) ){//判断用户名是否为空
        header("Location:login2.php");
    }
    else if ( !empty( $_SESSION[‘name‘] ) ){//用户名非空
        $sql = "SELECT * FROM user_info WHERE user_name = ‘{$_SESSION[‘name‘]}‘ AND user_pwd = ‘{$_SESSION[‘pwd‘]}‘";
        $res = $mysql->query( $sql );
        if( !( $res && mysql_num_rows( $res ) ) ){//用户名密码正确
            header("Location:login2.php");
        }
    }
    if( isset( $_GET[‘act‘] ) && $_GET[‘act‘] == ‘logout‘ ){//是否注销
        unset( $_SESSION[‘name‘] ); //移除用户名
        header("Location:login2.php");
    }    
?>
   <h3>欢迎您<?php echo !empty( $_SESSION[‘name‘] ) ? $_SESSION[‘name‘] : ‘‘; ?>! 登录会员中心</h3> 
   <a href="?act=logout">注销</a> 
</body>
</html>

5.在注册页面注册成功的时候保存session --->注册页面(新建reg.php)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .reg-form {
      margin-top: 100px;
    }
  </style>
</head>

<body>

  <?php
  session_start();
require( ‘./mysql.class.php‘ );
?>


    <div class="container">
      <div class="row">
        <div class="tip"></div>
        <form class="form-horizontal reg-form" role="form" method="post">
          <div class="form-group">
            <label for="firstname" class="col-md-2 control-label">用户名:</label>
            <div class="col-md-6">
              <input type="text" class="form-control" id="user" name="user" placeholder="请输入用户名">
            </div>
          </div>
          <div class="form-group">
            <label for="lastname" class="col-md-2 control-label">密码</label>
            <div class="col-md-6">
              <input type="password" name="pwd" class="form-control" id="pwd" placeholder="请输入密码">
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-primary">注册</button>
            </div>
          </div>
        </form>
      </div>
    </div>

    <?php
if( !empty( $_POST[‘user‘] ) && !empty( $_POST[‘pwd‘] ) ){
    $userName = $_POST[‘user‘];
    $userPwd = $_POST[‘pwd‘];
    $sql = ‘SELECT * FROM user_info WHERE user_name = "‘ . $userName . ‘"‘;
    $res = mysql_query( $sql );
    if( $res && mysql_num_rows( $res ) ){ //用户名存在
       ?>
        <script>
            document.querySelector(".tip").innerHTML = ‘你输入的用户名已经存在‘;
        </script> 
       <?php 
    }else { //用户名, 开始注册了
        $sql = "INSERT INTO user_info ( user_name, user_pwd ) VALUES( ‘$userName‘, ‘$userPwd‘ )";
        $res = mysql_query( $sql );
        if( $res !== false ){
            $_SESSION[‘name‘] = $userName;
            $_SESSION[‘pwd‘] = $userPwd;
            ?>
        <script>
            document.querySelector(".tip").innerHTML = ‘用户名注册成功‘;
        </script> 
            <?php
        }else {
            ?>
        <script>
            document.querySelector(".tip").innerHTML = ‘用户名注册失败‘;
        </script> 
            <?php
        }
    }
}
?>

</body>

</html>

  

 

 

 

  

前端小白之每天学习记录----php(7)session与cookie

标签:1.0   put   lang   添加   func   tco   web服务   location   zhang   

原文地址:http://www.cnblogs.com/szlxb/p/7367417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!