example.com/home.php?user=XX

Discussion in 'PHP' started by timallard, Feb 10, 2008.

  1. #1
    Hello,

    Im working on a website, where a user can have their own profile.

    I have a .home.php where the user can view their profile.
    To view other peoples profile, I would like it to be structured as follows:

    home.php?userid=2

    how would I go about doing that?

    Thank you for your help,
    -Tim
     
    timallard, Feb 10, 2008 IP
  2. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you could do it the easy way:

    
    $userid = $_GET["userid"];
    if(!is_numeric($userid) || $userid==""){
    $userid = logged in user id;
    }
    
    
    Code (markup):
    then use $userid in your queries
     
    imvain2, Feb 10, 2008 IP
    timallard likes this.
  3. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Hello,

    Here's some help for you, I took the liberty of writing an example for you below.

    <?php

    // Home.php

    if(isset($_GET['userid']) && !empty($_GET['userid']) && is_numeric($_GET['userid']))
    {

    $userid = trim($_GET['userid'];

    $sql = "SELECT * FROM users WHERE userid='".mysql_real_escape_string($userid)."'";

    $result = mysql_query($sql);
    $num = mysql_num_rows($result);
    $row = mysql_fetch_array($result);

    if($num == 1)
    {
    $firstname = stripslashes($row['firstname']);
    // Rest of the variables
    }


    }

    HTML DATA HERE

    Simply echo the data onto the page we retrieved where ever applicable.

    <html>
    <head>
    <title><?=$row['firstname']?>'s Home Page</title>
    </head>

    <body>
    <p>Welcome to <?php echo $row['firstname'];?>'s Home Page.</p>
    </body>

    </html>


    ?>
     
    dwayne12, Feb 10, 2008 IP
    timallard likes this.
  4. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #4
    Great, thank you both so much.
    Now as for building the url...how would that work?

    if i typed in example.com/home.php?user=1 how would it know to display userid 1's data?
    If the above example just answered my question, i do apologize, still learning :)
     
    timallard, Feb 10, 2008 IP
  5. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Yep the example I gave answered your question.

    The principle is the data is stored in the database and each user has a userid. You simply query the database with the userid like my example and retrieve the information for that user.

    You will of course have to ensure all data is secured..
     
    dwayne12, Feb 10, 2008 IP
  6. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #6
    My friend, you rock my socks. Thank you greatly.

    You did forget a closing parenthesis on line 8 tho. not trying to point that out, but for others who follow along may not see it.


    $userid = trim($_GET['userid']);

    +rep for all your help!

    works like a charm.
     
    timallard, Feb 10, 2008 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    Im having a small issue...

    when i switch between viewing profiles and my edit profile area, some variables are getting stuck as the other viewers and not my own.

    e.g.

    I'm logged in as "Tim" and view my profile. My userid is 1.
    I switch to see "Bryan's" profile. His user id is 2.
    I then switch back to view my edit profile area, but my userid is showing as "2" and not 1.

    how do i keep my variables mine, and their variables theirs?

    thanks for all your help!!
    -Tim
     
    timallard, Feb 11, 2008 IP
  8. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Are you setting some kind of cookie when the user views someone else's profile?

    If you are, you should only need to set the cookie for the logged in member, and use the querystring variable if available. My example actually showed this method.
     
    imvain2, Feb 11, 2008 IP
  9. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #9
    Thank you!...i missed a session variable...ah the small things.
     
    timallard, Feb 11, 2008 IP
  10. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #10
    I wouldn't use cookies for that type of data storage unless it's encrypted. Just use session variables and you will be absolutely fine. Good luck and let me know if you need any more help.

    - Dwayne.
     
    dwayne12, Feb 12, 2008 IP