SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot kevinritt's Avatar
    Join Date
    Nov 2002
    Location
    Lynn, Massachusetts-United States
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate dynamic data

    Hello,
    I have a website setup with a database. I have registered users that can login using a Username and Password. Each webpage is generated using a PHP template which includes the header, menu and footer. The body of the page is for the dynamic content. Here's my question:
    As each user logs in, how do I call a page that has just the info about that user? I know how to query the database for the data, but I/m not sure how to display that data on a different page for each user. Since the database will grow as users sign up, I can't create a static page for each user. That's the reason for the dynaimc generation of content based on the user's login.
    I hope I'm making sense here.
    Thanks for any help.

  2. #2
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please provide some code you have written that queries the database to retrieve user data. It will be easier for us to help you.

  3. #3
    SitePoint Zealot kevinritt's Avatar
    Join Date
    Nov 2002
    Location
    Lynn, Massachusetts-United States
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    /* get the incoming ID and password hash */
    $userName = $_POST["userName"];
    $pass = sha1($_POST["password"]);
    /* establish a connection with the database */
    $server = mysql_connect("localhost", "db_access",
    "********");
    if (!$server) die(mysql_error());
    mysql_select_db("myDB");

    /* SQL statement to query the database */
    $query = "SELECT * FROM users WHERE userName = '$userName'
    AND Password = '$password'";

    /* query the database */
    $result = mysql_query($query);

    /* Allow access if a matching record was found, else deny access. */
    if (mysql_fetch_row($result)) {
    header ('Location: http://www.domain.com/index.php');
    }
    else {
    header ('Location: http://www.domain.com/home.php');
    }
    mysql_close($server);
    ?>

    I wasn't sure how to color code the script. I guess what I'm looking for is something more advanced as far as pulling up the data for each user

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,271
    Mentioned
    197 Post(s)
    Tagged
    3 Thread(s)

    forum tools

    Quote Originally Posted by kevinritt
    I wasn't sure how to color code the script.
    Highlight the code and click on the PHP icon above the text area

  5. #5
    SitePoint Zealot kevinritt's Avatar
    Join Date
    Nov 2002
    Location
    Lynn, Massachusetts-United States
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
          
    /* get the incoming ID and password hash */
          
    $userName $_POST["userName"];
          
    $pass sha1($_POST["password"]);
          
    /* establish a connection with the database */
          
    $server mysql_connect("localhost""db_access",
                    
    "********");
          if (!
    $server) die(mysql_error());
          
    mysql_select_db("myDB");
            
          
    /* SQL statement to query the database */
          
    $query "SELECT * FROM users WHERE userName = '$userName'
                   AND Password = '
    $password'";
          
          
    /* query the database */
          
    $result mysql_query($query);
          
          
    /* Allow access if a matching record was found, else deny access. */
          
    if (mysql_fetch_row($result)) {
          
    header ('Location: http://www.domain.com/index.php');
          }
          else {
          
    header ('Location: http://www.domain.com/home.php');
          }
          
    mysql_close($server);  
          
    ?>

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,271
    Mentioned
    197 Post(s)
    Tagged
    3 Thread(s)

    unique dynamic content

    You're off to a good start. How about using $_SESSION vars to pull the content from the database into each page?

  7. #7
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True that. And as for your question about displaying user info for each user, you need to modify your script a little bit:
    PHP Code:
    <?php

    /* Allow access if a matching record was found, else deny access. */

    $record mysql_fetch_row($result);

    if (!
    $record)
    {
          
    header ('Location: http://www.domain.com/home.php');
          exit;
    }

    // If you come to this part of the script then the username and
    // password are correct and a row was returned.

    // Instead of redirecting the user to another page, you can
    // display his "profile" information right here.

    ?>

    <!-- Your html here -->

    <table ... >
       <tr>
          <td>Username:</td>
          <td><?php echo($record['userName']); ?></td>
       </tr>
       <tr>
          <td>Real Name:</td>
          <td><?php echo($record['realName']); ?></td>
       </tr>
    </table>

  8. #8
    SitePoint Zealot kevinritt's Avatar
    Join Date
    Nov 2002
    Location
    Lynn, Massachusetts-United States
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thanks.
    I'll try this and see if it works.


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
  •