欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

教你快速实现PHP全站权限验证

程序员文章站 2022-06-05 17:13:47
...

PHP全站权限验证代码示例:

  1. html>
  2. head> title>e
  3. /title>
  4. /head>
  5. body>
  6. form action="login.php"
    method="POST" >

用户:

  1. input type="text"
    name="username">
  2. br>

密码:

  1. input type="password"
    name="password">
  2. input type ="Submit"
    value ="确定">
  3. input type ="reset"
    value ="取消">
  4. /form>
  5. /body>
  6. /html>

----------------conn.php--------------------------

  1. ?php
  2. $host='127.0.0.1';
  3. $mysql_user='root';
  4. $mysql_password='qeephp';
  5. $mydb='learn';
  6. $link = mysql_connect($host,
    $mysql_user , $mysql_password)
  7. or die("无法连接数据库: "
    . mysql_error());
  8. mysql_select_db($mydb);
  9. ?>

----------------------------login.php------------------

  1. ?
  2. $username=$_POST['username'];
  3. $password=$_POST['password'];
  4. if ($username==""){
  5. echo " script language='javascript'>
    alert('非法操作!');
    location.href=
    'index.php'; /script>";
  6. exit;
  7. }
  8. require_once("conn.php");
  9. $sql="SELECT * FROM admin where
    username='$username'"
    ;
  10. $result=mysql_query($sql);
  11. $row=mysql_fetch_array($result);
  12. if ($row['username']==""){
  13. echo " script language='javascript'>
    alert('用户名有误!');
    location.href=
    'index.php'; /script>";
  14. exit;
  15. }else if ($row['password']!=$password){
  16. echo " script language='javascript'>
    alert('密码有误!');
    location.href='index
    .php'
    ; /script>";
  17. echo "密码有误";
  18. exit;
  19. }else{
  20. session_start();
  21. $_SESSION['user']=$username;
  22. echo " script language='javascript'>
    alert('登陆成功!');
    location.href=
    'list.php'; /script>";
  23. }
  24. ?>

------------------check.php--------------------

  1. ?
  2. session_start();
  3. if(! isset($_SESSION['user']))
  4. {
  5. echo " script language='javascript'>
    alert('非法操作!');
    location.href=
    'index.php'; /script>";
  6. exit;
  7. }else{
  8. echo "欢迎".$_SESSION['user']
    ."登录系统";
  9. }
  10. ?>

---------------------list.php-----------------

  1. ?
  2. include 'check.php';
  3. ?>
  4. html>
  5. head>
  6. script type="text/javascript"
    src="player/swfobject.js"> /script>
  7. /head>
  8. body>
  9. h3>单个文件播放: /h3>
  10. p id="player1"> a href="
    http://www.macromedia.com/go/
    getflashplayer"
    >获取播放器 /a> 观看电影 /p>
  11. script type="text/javascript">
  12. var s1 = new SWFObject("player/
    flvplayer.swf","single","300","170","7");
  13. s1.addParam("allowfullscreen","true");
  14. s1.addVariable("file","player/ad.flv");
  15. s1.addVariable("image","player/preview.jpg");
  16. s1.addVariable("width","300");
  17. s1.addVariable("height","170");
  18. s1.write("player1");
  19. /script>
  20. h3>playlist file, with different
    colors:
    /h3>
  21. p id="player2"> a href="http:
    //www.macromedia.com/go/getflashplayer"
    >
    Get the Flash Player /a> to see this player. /p>
  22. script type="text/javascript">
  23. var s2 = new SWFObject("player/flvplayer
    .swf","playlist","300","312","7");
  24. s2.addParam("allowfullscreen","true");
  25. s2.addVariable("file","player/playlist.xml");
  26. s2.addVariable("displayheight","200");
  27. s2.addVariable("backcolor","0x000000");
  28. s2.addVariable("frontcolor","0xCCCCCC");
  29. s2.addVariable("lightcolor","0x557722");
  30. s2.addVariable("width","300");
  31. s2.addVariable("height","312");
  32. s2.write("player2");
  33. /script>
  34. /body>
  35. /html>

--------------------logout.php-------------------------

  1. ?php
  2. unset($_SESSION['user']);
  3. unset($_SESSION['password']);
  4. echo "注销成功";
  5. ?>

以上就是PHP全站权限验证的具体实现方法。