Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Changing Contents via URL

    Is there a way I can add a URL attribute (like ?user=connorjack) and it will change what is on the page. For example, if I have a page with some embed code in. Part of the embed code includes a username, so I want to change that via the URL rather than create duplicate pages thousands of time.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    At the top of your page ...
    PHP Code:
    <?php
    $user
    =$_GET['user'];

    // now, you can use the $user variable anywhere you want in your HTML (see below).

    ?>
    <html>
    ... your html page stuff

    ... wherever your embed code is ....

    <blah blah   <?=$user?>   blah blah>

    </html>

  • Users who have thanked mlseim for this post:

    ConnorJack (10-09-2009)

  • #3
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    to expand
    PHP Code:
    //url = users.php?user=testing
    $user $_GET['user'];

    if(isset(
    $user)) {
        
    /* you should validate the variable to prevent malicious input */
        
    $query 'SELECT * FROM `users` WHERE username="' $user '" LIMIT 1';
        
    $result=mysql_query($query);
        
    $r=mysql_fetch_array($result);
        echo 
    '<h2>Viewing: ' $r['username'] . ' Profile</h2>';
        
    // etc

    } else {

        echo 
    '<p>Please select a user...</p>';
        
    $qry mysql_query('SELECT * FROM `users` ORDER BY id ASC');
        while(
    $r=mysql_fetch_array($qry)) {
           echo 
    '<a href="users.php?user='.$r['username'].'">'.$r['username'].'</a><br />';
        }

    Last edited by met; 10-09-2009 at 07:39 PM.

  • #4
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    At the top of your page ...
    PHP Code:
    <?php
    $user
    =$_GET['user'];

    // now, you can use the $user variable anywhere you want in your HTML (see below).

    ?>
    <html>
    ... your html page stuff

    ... wherever your embed code is ....

    <blah blah   <?=$user?>   blah blah>

    </html>
    Great! Thanks! And I can change the variable with "?user=username"?
    Last edited by ConnorJack; 10-09-2009 at 07:57 PM.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    That's right,

    If you do this, "http://www.mywebsite.com?user=connorjack",
    the variable called $user will contain the value of "connorjack".

    As Met is alluding to ...
    Anything you bring into your script should be "sanitized" so that nothing
    malicious can be injected. Bringing in a username, like "connorjack" poses
    no problems as we can see, but we don't know what the rest of your script
    is doing (as we can't see it of course), so just be careful about what you
    allow and how the variable is used.

    Your webpage (or script) needs to either be .php (instead of .html),
    or you have to instruct your webhost server to process .html as a PHP script.
    Since you posted this in the PHP forum, I assume you already know about PHP.
    Last edited by mlseim; 10-09-2009 at 08:22 PM.

  • #6
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    That's right,

    If you do this, "http://www.mywebsite.com?user=connorjack",
    the variable called $user will contain the value of "connorjack".

    As Met is alluding to ...
    Anything you bring into your script should be "sanitized" so that nothing
    malicious can be injected. Bringing in a username, like "connorjack" poses
    no problems as we can see, but we don't know what the rest of your script
    is doing (as we can't see it of course), so just be careful about what you
    allow and how the variable is used.

    Your webpage (or script) needs to either be .php (instead of .html),
    or you have to instruct your webhost server to process .html as a PHP script.
    Since you posted this in the PHP forum, I assume you already know about PHP.
    Yeah, thanks for your help. It is working. I do know HTML, CSS and a little PHP. But I only started with MySQL a few days ago.


  •  

    Posting Permissions

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