Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Member #
    33229
    Hello,

    I'm using a simple comment system that is using the name, email, and the body fields when posting a comment, but I have removed the name field and kept the body because I made it only for registered users to post comments, but now after the user logs in, he can post, but I couldn't figure out how to pull the user name from the DB to put it in the post.

    I appreciate any help.:happy:

    Regards.
    (Sorry for posting it again, I posted it in the wrong section.)

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Need more information.

    Is this a system you installed from an existing script (like WordPress or some CMS)?
    Are you using MySQL?
    You might have to show us some of the scripting.

    Give more info on this.


  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Member #
    33229
    Its not a ready script, but its from a tutorial, Jquery comment system.
    Yes I'm using MySQl.

    This is the comment code

    PHP Code:
                    <div class="name">'.$link_open.$d['name'].$link_close.'</div>
                    <
    div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
                    <
    p>'.$d['body'].'</p
    The $d['name'] is supposed to be the name from the name field, but since I added the Registration system I had to remove it, and now I cant figure out how to add the Poster/User name.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    So are you saying that when the user registers, they entered their name.
    And that is the only time they enter their name? So, you do have
    their name in the database. Is that correct?

    If you have their name in the database (from when they registered), you
    can deal with the name at the time of "logging-in". When a person logs in,
    PHP sets a SESSION (most likely their user ID). You would also set another
    SESSION with their "name". When they post, PHP will put their name as
    the person who posted.

    If you think I'm on the right track, post the actual login script so I can
    see where the login SESSION is getting set.


  6. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Member #
    33229
    Yes, the names are in the Database, and here is the login code.

    Code:
    <?php
    
    define('INCLUDE_CHECK',true);
    
    require 'membership/connect.php';
    require 'membership/functions.php';
    // Those two files can be included only if INCLUDE_CHECK is defined
    
    session_name('tzLogin');
    // Starting the session
    
    session_set_cookie_params(2*7*24*60*60);
    // Making the cookie live for 2 weeks
    
    session_start();
    
    if($_SESSION['id'] && !isset($_COOKIE['tzRemember']) && !$_SESSION['rememberMe'])
    {
        // If you are logged in, but you don't have the tzRemember cookie (browser restart)
        // and you have not checked the rememberMe checkbox:
    
        $_SESSION = array();
        session_destroy();
    
        // Destroy the session
    }
    
    if(isset($_GET['logoff']))
    {
        $_SESSION = array();
        session_destroy();
    
        header("Location: index.php");
        exit;
    }
    
    if($_POST['submit']=='Login')
    {
        // Checking whether the Login form has been submitted
    
        $err = array();
        // Will hold our errors
    
        if(!$_POST['username'] || !$_POST['password'])
            $err[] = 'All the fields must be filled in!';
    
        if(!count($err))
        {
            $_POST['username'] = mysql_real_escape_string($_POST['username']);
            $_POST['password'] = mysql_real_escape_string($_POST['password']);
            $_POST['rememberMe'] = (int)$_POST['rememberMe'];
    
            // Escaping all input data
    
            $row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM tz_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));
    
            if($row['usr'])
            {
                // If everything is OK login
    
                $_SESSION['usr']=$row['usr'];
                $_SESSION['id'] = $row['id'];
                $_SESSION['rememberMe'] = $_POST['rememberMe'];
    
                // Store some data in the session
    
                setcookie('tzRemember',$_POST['rememberMe']);
            }
            else $err[]='Wrong username and/or password!';
        }
    
        if($err)
        $_SESSION['msg']['login-err'] = implode('<br />',$err);
        // Save the error messages in the session
    
        header("Location: index.php");
        exit;
    But I tried using $_SESSION['usr'] instead of the $d['name']
    but it will show all the post by my name, and when i logout the names will be removed, so i think
    using the $_SESSION['usr'] is wrong.

    Regards.

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    I'm still confused about something ...

    You said you took out the name field when they post,
    but now a logged-in person can post. When they do,
    what gets put into the post (for the name)?

    Are the posts being written now without any author names?

    I'm thinking it's possible now that for every post that
    gets written, there is no name associated with it.
    Let me know if that's the case.


  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Member #
    33229
    Yes, the posts are written without any author name.

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    OK ...

    That has to be put back in, but not as a text box.

    At the time that the form is displayed, where the user enters their post,
    you used to have the text box for the user's name. Instead of the
    text box, replace it with a hidden type:

    <input type="hidden" name="author" value="<?=$name?>">

    I don't know the name of the variable, so in my example I just put "author".
    The name would be the same as the previous text box name.

    ==

    You now need to know their name. You already have that when they
    logged-in ... the SESSION set as 'usr'. So before the form is displayed ...

    At the top of the page or script where the post form appears ...

    <?php
    session_start();

    if(isset($_SESSION['usr'])){
    $name=$_SESSION['usr'];
    }
    ?>

    Then in the form:
    <input type="hidden" name="author" value="<?=$name?>">

    When the form is submitted, instead of the textbox where they used
    to enter their name, it is now automatically entered as their username.

    You didn't show the form, so use the correct variable name (probably not "author").

    The Summary: You need to have their username submitted with each post.
    Just like the original script did with the textbox. Now you do that with a hidden
    variable that is their username (as when they logged in).


  10. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Member #
    33229
    I did what you said, but it showed the name until I logged out, when i logged off, the name disappeared.
    I also have this validation code.

    PHP Code:
                    if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
            {
                
    $errors['name'] = 'Please enter a name.';
            } 
    Regards.

  11. #10
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    That would be strange.

    If I log-in and want to make a post,
    a form appears where I type it in.

    I type in my post and submit it.

    Doesn't the name get written to the database with each post?
    Isn't that the way it worked when you originally had a text box?



Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:42 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com