Creating "profile" pages with $_GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fmjnax
Forum Newbie
Posts: 1
Joined: Wed May 26, 2010 11:45 am

Creating "profile" pages with $_GET

Post by fmjnax »

My apologies for the obscure topic title, but I wasn't sure exactly what to call it. I'm fairly new to PHP.

I have a page which I want to be able to generate a random ID for when the users click save. It will then give them a profile ID in the url (example: myurl.php?profile=12345) so that they can bookmark/return without having to remember their ID or having to perform a "login" task. Now, what I want to be able to do is extract that 12345 and have it populate the page based on their saved values in the MySQL database. I've got all of that work done. However, I think my approach to extracting the GET may be incorrect.

I have my main page in which I call out to another php in order to populate the user values section of the page. I currently have it set up as a require() page, but it's complaining about 'No such file or directory'. Below is the require() on the main page:

Code: Select all

<h2>[ User Values ]</h2>
<? require("$_SERVER[DOCUMENT_ROOT]/user_fields.php?profile=$_GET[profile]"); ?>
And in the user_fields.php, just for testing purposes:

Code: Select all

<?
	$attack_table .= $_GET['profile'];
?>
<?= $attack_table; ?>
Any ideas? Thanks in advance!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Creating "profile" pages with $_GET

Post by requinix »

Whatever you give to require() et al. has to be an actual file. URLs are often not files.

In your case, since $_GET[profile] has the right value, you can just include the file without having to do any extra work.

Code: Select all

<?php require("$_SERVER[DOCUMENT_ROOT]/user_fields.php"); ?>
Please use the full opening tag "<?php" whenever possible.
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: Creating "profile" pages with $_GET

Post by phu »

If that include works at all, it's only because PHP treats undefined constants as their string value, which is not good to depend on. Besides, if you're using an array index like that in a double-quoted string, you should enclose it in braces.

Code: Select all

<?php require("{$_SERVER['DOCUMENT_ROOT']}/user_fields.php"); ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Creating "profile" pages with $_GET

Post by requinix »

phu wrote:If that include works at all
It works because it uses the correct syntax. Check the manual page on string parsing again.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Creating "profile" pages with $_GET

Post by John Cartwright »

phu wrote:If that include works at all, it's only because PHP treats undefined constants as their string value, which is not good to depend on. Besides, if you're using an array index like that in a double-quoted string, you should enclose it in braces.

Code: Select all

<?php require("{$_SERVER['DOCUMENT_ROOT']}/user_fields.php"); ?>
When arrays are evaluated in braces, they do not need quotes around the keys.

I.e.,

Code: Select all

<?php require("{$_SERVER[DOCUMENT_ROOT]}/user_fields.php"); ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Creating "profile" pages with $_GET

Post by requinix »

Sigh.

Lemme quote the manual page. Simple syntax:

Code: Select all

<?php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote array string keys and do not use
// {braces}.

// Show all errors
error_reporting(E_ALL);

$fruits = array('strawberry' => 'red', 'banana' => 'yellow');

// Works, but note that this works differently outside a string
echo "A banana is $fruits[banana].";

// Works
echo "A banana is {$fruits['banana']}.";

// Works, but PHP looks for a constant named banana first, as described below.
echo "A banana is {$fruits[banana]}.";

// Won't work, use braces.  This results in a parse error.
echo "A banana is $fruits['banana'].";

// Works
echo "A banana is " . $fruits['banana'] . ".";

// Works
echo "This square is $square->width meters broad.";

// Won't work. For a solution, see the complex syntax.
echo "This square is $square->width00 centimeters broad.";
?>
Complex/curly syntax:

Code: Select all

<?php
// Show all errors
error_reporting(E_ALL);

$great = 'fantastic';

// Won't work, outputs: This is { fantastic}
echo "This is { $great}";

// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";

// Works
echo "This square is {$square->width}00 centimeters broad."; 

// Works
echo "This works: {$arr[4][3]}";

// This is wrong for the same reason as $foo[bar] is wrong  outside a string.
// In other words, it will still work, but only because PHP first looks for a
// constant named foo; an error of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}"; 

// Works. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][3]}";

// Works.
echo "This works: " . $arr['foo'][3];

echo "This works too: {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Creating "profile" pages with $_GET

Post by John Cartwright »

Ah, your right.. it's been awhile since I've actually used that syntax (or much PHP for that matter).

P.S., Where I come from "sigh"ing someone in a discussion isn't very polite.
Post Reply