Discuz!官方免费开源建站系统

 找回密码
 立即注册
搜索

[求助] 如何判断用户是否已经登录?

[复制链接]
jilingshu 发表于 2007-11-14 12:47:17 | 显示全部楼层 |阅读模式
我想写一个程序,要求做到基本的访问控制功能。现在的要求是:
用户访问 main.php 页面后,页面判断用户是否已经登录论坛。若没有登录,则转向一个URL,若已登录则转向另一个URL。
我知道DZ用的是cookie,但是我不知道应该如何判断登录状态........

别告诉我用passport...我不想让论坛与我的程序反向整合。我只要判断登录状态就可以了。

我php很烂,最好能给段示例代码

谢啦~

在线等...
回复

使用道具 举报

 楼主| jilingshu 发表于 2007-11-14 13:21:53 | 显示全部楼层
有人知道吗?
回复

使用道具 举报

LuciferSheng 发表于 2007-11-14 14:48:21 | 显示全部楼层
  1. <?
  2. require_once './include/common.inc.php';

  3. if($discuz_uid){
  4.        header("location:已经登录的跳转地址") ;
  5. }else{
  6.        header("location:没登录的跳转地址") ;
  7. }
  8. ?>
复制代码
回复

使用道具 举报

 楼主| jilingshu 发表于 2007-11-14 20:42:44 | 显示全部楼层
ok这个解决了
还有个问题
我想自己写个表单,让用户输入用户名和密码,然后实现dz的登录。
我在本站找到了这个:

  1. <?php
  2. //discuz同服务器下无passport登陆整合方法
  3. //贡献:huozhe3136 QQ:2666556
  4. //假设discuz安装在网站根目录下的bbs/中
  5. //用法事例
  6. error_reporting(E_ALL);

  7. $path_bbs="bbs/";//请修改为你的论坛的相对路径
  8. $check_username="admin";//测试时这个用户名必须在论坛里也存在
  9. $check_password="123456";
  10. define('IN_DISCUZ', TRUE);//为了包含discuz下的文件这个必须定义
  11. require_once "$path_bbs/config.inc.php";//获得论坛数据库的配置
  12. require_once "$path_bbs/include/db_mysql.class.php";

  13. if(@$_GET[act]=="logout"){
  14.     foreach ($_COOKIE as $key => $value) {
  15.         setcookie($key,'',time() - 3600,$cookiepath,$cookiedomain,0);            
  16.             header("Location:".$_SERVER['PHP_SELF']);         
  17.     }
  18. }
  19. if(isset($_POST["submit"])){//检查是否点击了提交按钮
  20.     $username=trim($_POST['username']);
  21.     $pwd=trim($_POST['password']);
  22.     //主站验证登陆,为简单起见,这里只作简单的示范
  23.     if($username==$check_username && $pwd==$check_password){//测试时这个用户名必须在论坛里也存在
  24.         $db = new dbstuff;
  25.         $db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);//$dbhost,$dbuser,$dbpw,$dbname,$pconnect是在bbs/config.inc.php 里定义的变量
  26.         $sql="select * from {$tablepre}members where username='$username'";
  27.         $rs = $db->query($sql);
  28.         if($dzmember = $db->fetch_array($rs)){
  29.             setcookie('myusername',$username,time()+3600,$cookiepath,$cookiedomain,0);//设置主站自身的cookie
  30.             setcookie("{$tablepre}sid",'',time() - 3600,$cookiepath,$cookiedomain,0);//让discuz的sid过期是为了让discuz重新分配一个新的sid
  31.             setcookie("{$tablepre}auth", authcode("$dzmember[password]\t$dzmember[secques]\t$dzmember[uid]", 'ENCODE'),time()+3600,$cookiepath,$cookiedomain,0);//$cookiepath和$ cookiedomainbbs/config.inc.php里定义的变量
  32.             header("Location:".$_SERVER['REQUEST_URI']);
  33.         }
  34.     }
  35. }

  36. //下面这个函数是discuz的用户cookie编码函数,已修改,原型在$path_bbs/include/global.func.php
  37. function authcode($string, $operation) {
  38.     global $_SERVER, $_DCACHE,$path_bbs;
  39.     require_once "$path_bbs/forumdata/cache/cache_settings.php";//需要获取论坛的authkey
  40.     $discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
  41.     $coded = '';
  42.     $keylength = strlen($discuz_auth_key);
  43.     $string = $operation == 'DECODE' ? base64_decode($string) : $string;
  44.     for($i = 0; $i < strlen($string); $i += $keylength) {
  45.         $coded .= substr($string, $i, $keylength) ^ $discuz_auth_key;
  46.     }
  47.     $coded = $operation == 'ENCODE' ? str_replace('=', '', base64_encode($coded)) : $coded;
  48.     return $coded;
  49. }
  50. ?>
  51. <?php
  52. if(@$_COOKIE['myusername']==''){
  53. ?>
  54. <form action="" method="POST">
  55. 用户名:<input name="username">测试用户名为admin,请更改为你的用户名<br>
  56. 密码:<input type="password" name="password">测试密码为123456<br>
  57. <input type="submit" name="submit" value="测试">
  58. </form>
  59. <?php
  60. }
  61. else echo("登陆成功,<a href='{$path_bbs}index.php' target=_blank>请到论坛检查登陆</a><br><a href='?act=logout'>退出登陆</a> ");
  62. ?>
复制代码

但是这段代码却无法工作
我看了下,cookie确实已经设置了cdb_auth和cdb_sid。这段代码的 登出 是有效的,但是登录却无法使用。程序提示说成功,但是dz却仍然显示为未登录。
应该怎么办?谢啦~!
在线等...
回复

使用道具 举报

 楼主| jilingshu 发表于 2007-11-15 00:43:12 | 显示全部楼层
没人回答?
回复

使用道具 举报

习明 发表于 2007-11-15 00:51:16 | 显示全部楼层
回复

使用道具 举报

 楼主| jilingshu 发表于 2007-11-15 11:48:52 | 显示全部楼层
很不幸,这段代码并不符合我的需求...
我的目的是用户点击 登录 后,能够登录discuz,但是并不跳转到discuz,而是我指定的一个页面。
似乎discuz并没有提供实现这个功能的方法。所以我才准备自己写的。
回复

使用道具 举报

习明 发表于 2007-11-15 12:06:34 | 显示全部楼层
<html>
        <head>
        <title>login</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        </head>
       
        <body>
       
        <form method="post" action="logging.php?action=login" target="_blank">
          <input type="hidden" name="referer" value="https://discuz.dismall.com/index.php">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td>用户名</td>
              <td>
                <input type="text" name="username">
              </td>
            </tr>
            <tr>
              <td>密码</td>
              <td>
                <input type="password" name="password">
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <input type="submit" name="loginsubmit" value="登录">
                <input type="reset" name="reset" value="重置">
                <input type="button" value="注册" onclick="javascript: this.form.action='register.php';this.form.submit();">
                <input type="button" value="游客" onclick="javascript: this.form.action='index.php';this.form.submit();">
              </td>
            </tr>
          </table>
        </form>
       
        </body>
        </html>
回复

使用道具 举报

习明 发表于 2007-11-15 12:09:30 | 显示全部楼层
很不幸 你没了解工作原理就动手了
回复

使用道具 举报

 楼主| jilingshu 发表于 2007-11-15 12:55:36 | 显示全部楼层
呃....我错了.........

那么为什么我自己通过文档里提供的函数生成一个cdb_auth就不行呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2025-12-7 18:22 , Processed in 0.088460 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表