Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2009, 07:16 PM   PM User | #1
ConnorJack
New Coder

 
Join Date: Oct 2009
Location: UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
ConnorJack is an unknown quantity at this point
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.
ConnorJack is offline   Reply With Quote
Old 10-09-2009, 07:29 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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>
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
ConnorJack (10-09-2009)
Old 10-09-2009, 07:34 PM   PM User | #3
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
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..
met is offline   Reply With Quote
Old 10-09-2009, 07:36 PM   PM User | #4
ConnorJack
New Coder

 
Join Date: Oct 2009
Location: UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
ConnorJack is an unknown quantity at this point
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..
ConnorJack is offline   Reply With Quote
Old 10-09-2009, 08:20 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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..
mlseim is offline   Reply With Quote
Old 10-09-2009, 08:25 PM   PM User | #6
ConnorJack
New Coder

 
Join Date: Oct 2009
Location: UK
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
ConnorJack is an unknown quantity at this point
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.
ConnorJack is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.