2010-05-31 60 views
0

我使用的登录系统运行良好。我也在使用评论系统。注释功能不会显示,除非用户登录(如下面的commentformonoff.php所示)。用户在发表第一条评论后退出登录

当用户发表评论时,信息从函数“show_commentbox”传递到文件comments2a.php。然后,信息被传递给文件comments2.php。

当网站首次在浏览器上拉起时,登录并发表评论后,用户已注销。在同一浏览器会话中第二次登录后,用户在发表评论后不再注销。

如何让用户在发表第一条评论后保持登录状态?

由于提前,

约翰

登录功能:

function show_loginform($disabled = false) 
{ 

    echo '<form name="login-form" id="login-form" method="post" action="./index.php?'.$_SERVER['QUERY_STRING'].'"> 

    <div class="usernameformtext"><label title="Username">Username: </label></div> 
    <div class="usernameformfield"><input tabindex="1" accesskey="u" name="username" type="text" maxlength="30" id="username" /></div> 


    <div class="passwordformtext"><label title="Password">Password: </label></div> 
    <div class="passwordformfield"><input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password" /></div> 


    <div class="registertext"><a href="http://www...com/.../register.php" title="Register">Register</a></div> 
    <div class="lostpasswordtext"><a href="http://www...com/.../lostpassword.php" title="Lost Password">Lost password?</a></div> 

    <p class="loginbutton"><input tabindex="3" accesskey="l" type="submit" name="cmdlogin" value="Login" '; 
    if ($disabled == true) 
    { 
     echo 'disabled="disabled"'; 
    } 
    echo ' /></p></form>'; 


} 

Commentformonoff.php:

<?php 
if (!isLoggedIn()) 
{ 
    if (isset($_POST['cmdlogin'])) 
    { 
     if (checkLogin($_POST['username'], $_POST['password'])) 
     { 
      show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl); 
     } else 
     { 
      echo "<div class='logintocomment'>Login to comment</div>"; 

     } 
    } else 
    { 

     echo "<div class='logintocomment'>Login to comment</div>"; 
    } 

} else 
{ 
    show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl); 
} 
?> 

功能 “show_commentbox”:

function show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl) 
{ 
echo '<form action="http://www...com/.../comments/comments2a.php" method="post"> 
    <input type="hidden" value="'.$_SESSION['loginid'].'" name="uid"> 
    <input type="hidden" value="'.$_SESSION['username'].'" name="u"> 
    <input type="hidden" value="'.$submissionid.'" name="submissionid"> 
    <input type="hidden" value="'.stripslashes($submission).'" name="submission"> 
    <input type="hidden" value="'.$url.'" name="url"> 
    <input type="hidden" value="'.$submittor.'" name="submittor"> 
    <input type="hidden" value="'.$submissiondate.'" name="submissiondate"> 
    <input type="hidden" value="'.$countcomments.'" name="countcomments"> 
    <input type="hidden" value="'.$dispurl.'" name="dispurl"> 



    <label class="addacomment" for="title">Add a comment:</label> 

    <textarea class="checkMax" name="comment" type="comment" id="comment" maxlength="1000"></textarea> 

    <div class="commentsubbutton"><input name="submit" type="submit" value="Submit"></div> 
</form> 
'; 
} 

包括在comments2a.php:

$uid = mysql_real_escape_string($_POST['uid']); 
$u = mysql_real_escape_string($_POST['u']); 

$query = sprintf("INSERT INTO comment VALUES (NULL, %d, %d, '%s', NULL)", $uid, $subid, $comment); 

mysql_query($query) or die(mysql_error()); 

$lastcommentid = mysql_insert_id(); 
header("Location: comments2.php?submission=".$submission."&submissionid=".$submissionid."&url=".$url."&submissiondate=".$submissiondate."&comment=".$comment."&subid=".$subid."&uid=".$uid."&u=".$u."&submittor=".$submittor."&countcomments=".$countcomments."&dispurl=".$dispurl."#comment-$lastcommentid"); 
exit(); 

包括在comments2.php:

if($_SERVER['REQUEST_METHOD'] == "POST"){header('Location: http://www...com/.../comments/comments2.php?submission='.$submission.'&submissionid='.$submissionid.'&url='.$url.'&submissiondate='.$submissiondate.'&submittor='.$submittor.'&countcomments='.$countcomments.'&dispurl='.$dispurl.'');} 

$uid = mysql_real_escape_string($_GET['uid']); 
$u = mysql_real_escape_string($_GET['u']); 

编辑:有人说,这可能是有用的,所以我张贴。

function isLoggedIn() 
{ 

    if (session_is_registered('loginid') && session_is_registered('username')) 
    { 
     return true; // the user is loged in 
    } else 
    { 
     return false; // not logged in 
    } 

    return false; 

} 

function checkLogin($u, $p) 
{ 
global $seed; // global because $seed is declared in the header.php file 

    if (!valid_username($u) || !valid_password($p) || !user_exists($u)) 
    { 
     return false; // the name was not valid, or the password, or the username did not exist 
    } 

    //Now let us look for the user in the database. 
    $query = sprintf(" 
     SELECT loginid 
     FROM login 
     WHERE 
     username = '%s' AND password = '%s' 
     AND disabled = 0 AND activated = 1 
     LIMIT 1;", mysql_real_escape_string($u), mysql_real_escape_string(sha1($p . $seed))); 
    $result = mysql_query($query); 
    // If the database returns a 0 as result we know the login information is incorrect. 
    // If the database returns a 1 as result we know the login was correct and we proceed. 
    // If the database returns a result > 1 there are multple users 
    // with the same username and password, so the login will fail. 
    if (mysql_num_rows($result) != 1) 
    { 
     return false; 
    } else 
    { 
     // Login was successfull 
     $row = mysql_fetch_array($result); 
     // Save the user ID for use later 
     $_SESSION['loginid'] = $row['loginid']; 
     // Save the username for use later 
     $_SESSION['username'] = $u; 
     // Now we show the userbox 
     return true; 
    } 
    return false; 
} 
+0

Apache应用程序是否在Debian服务器上提供php应用程序? – 2010-05-31 11:29:11

+0

这是在Unix宿主。我不确定Apache2或Debian。我是新手。 – John 2010-05-31 11:32:11

回答

0

这将是很高兴看到更多关于发生了什么事情。这些只是你认为可能很重要的片段,而不是全部。

有一些与您提交的代码相关的问题: - commentformonoff.php如何连接到您提交的其他php文件? - 在isLoggedIn()和checkLogin()函数中会发生什么? - 为什么你将功能分成comments2.php和comments2a.php?没有理由的重定向只会增加执行的延迟。有没有理由不能在那里处理请求? - comment2a.php中的评论值直接进入查询而没有卫生,这是一个严重的安全漏洞。 - 在comments2a.php中,创建一个重定向并通过GET传递变量,并在comments2.php中检查POST和重定向,如果发现一个post请求。你为什么这么做?

退房Smarty如果可以的话,那不是一个很大的开销,你不必编写吐出html表单的函数。或者,如果里面没有参数,可以直接在代码中包含html代码,并关闭并重新打开php标签。

1

我认为你的错误是在isLoggedIn()你可以发布这个。因为你有两条写评论框的路径。这可能意味着登录时会选择第一条路径,但在刷新时,如果您希望到达第二条路径,则不会。

错误也可能在checkLogin中,而不是设置会话变量?

两个isLoggedIn()和checkLogin(),请张贴:)


<?php 
if (!isLoggedIn()) // most likely the place of error 
{ 
    if (isset($_POST['cmdlogin'])) 
    { 
     if (checkLogin($_POST['username'], $_POST['password'])) // setting session variable correctly? 
     { 
      // path one 
      // are you supposed to set some session variables here? or in checkLogin()? 
      show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl); 
     } else 
     { 
      echo "Login to comment"; 

     } 
    } else 
    { 

     echo "Login to comment"; 
    } 

} else 
{ 
    // path two 
    show_commentbox($submissionid, $submission, $url, $submittor, $submissiondate, $countcomments, $dispurl); 
} 
?> 

编辑: 在isLoggedIn()使用isset()函数,而不是session_is_registered()。 session_is_registered()自PHP 5.3.0起弃用。 if(isset($_SESSION['loginid']) && isset($_SESSION['username'])

在文件CommentOnOff.php的底部可以放入这段代码吗? var_dump($_SESSION) 它应该打印会话中包含的所有内容。然后你可以看到loginind和用户名是否实际存储在会话中:)

+0

好的,谢谢。我发布了isLoggedIn()和checkLogin()。 – John 2010-05-31 12:29:09

+0

为你修改答案:) – 2010-05-31 13:15:32

+0

嗯......当我尝试登录时,将isLoggedIn()更改为isset()时会出现一个空白页面。 – John 2010-06-02 09:14:46

0

我在开发Web应用时出现了非常类似的症状。

尝试添加一个favicon.ico文件(一个空的确定)到您的应用程序的根目录。出现 “登录”,在

用户日志,第一页:

这些是我正在经历的症状...

Firefox浏览器。用户单击链接并不再登录。用户再次登录并首先“登录”页面。用户单击链接并仍然登录。用户继续使用该应用程序作为登录用户没有问题。页面出现

用户登录时,第一个 “登录”:

铬。用户单击链接并不再登录。用户再次登录并首先“登录”页面。用户点击链接并重新注销。用户在首次“登录”页面后无法保持登录状态。

我检查了错误日志,看到每个请求都在寻找favicon.ico文件。我在应用程序根目录中添加了一个空的favicon.ico文件,问题停止了。

+2

那个你需要多解释一下。它让我哭笑不得。这有助于什么?其他然后从您的访问日志中删除大量的404 – 2010-05-31 12:13:26

+0

是的,它确实看起来很奇怪。它解决了我的问题让我感到惊讶,但它确实如此。我开发的应用程序在我的开发机器上运行良好(没有favicon.ico),所以我知道我的代码很好。我花了一些时间尝试php.ini设置,并且作为最后的手段,我将favicon.ico添加到导致问题保持错误日志的服务器上。突然间,我的登录/注销问题得到解决。 – 2010-05-31 12:38:46

相关问题