SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    User Getting Logged Out After Making First Comment

    Hello,

    I am using a login system that works well. I am also using a comment system. The comment function does not show up unless the user is logged in (as shown in commentformonoff.php below).

    When a user makes a comment, the info is passed from the function "show_commentbox" to the file comments2a.php. Then, the info is passed to the file comments2.php.

    When the site is first pulled up on a browser, after logging in and making a comment, the user is logged out. After logging in a second time during the same browser session, the user is no longer logged out after making a comment.

    How can I keep the user logged in after making the first comment?

    Thanks in advance,

    John

    Login function:

    Code:
    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:

    Code:
    <?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);
    }
    ?>
    Function "show_commentbox":

    Code:
    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>
    '; 
    }
    Included in comments2a.php:

    Code:
    $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();
    Included in comments2.php:

    Code:
    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']);

  2. #2
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try commenting out
    PHP Code:
    exit(); 
    in comments2a.php. It may be destroying your session.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Exit is standard practice after a header redirect, and has no effect on the session as far as I'm aware.

    Are you sure that you're using session_start in the further file?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •