Hey everyone. Ive been trying to set up a link that will allow people to see their profiles but I cant get the link to work. I have a center column that on each click gets the variable $page and loads the correct page using an include("profile.php"); Now I need the link to look like this:
echo "<a href=\"index.php?page=userinfo?user=$session->username\">Edit Account</a>";
Where the var page is set to userinfo and the var user is set to the current user logged in. I know the above syntax isnt correct but I dont know how else to show everyone what I want accomplished. Any suggestions?? Thanks!
The syntax of the php code looks ok. But as it is, PHP doesn't know for sure if $session->username is a variable in the string. You may want to surround your variables with {} to make this clear to the PHP parser. Eg:
echo "<a href=\"index.php?page=userinfo?user={$session->username}\">Edit Account</a>";
For this particular link however, it does not make sense to pass the username in the URL.
Normally you'd go with:
echo "<a href=\"index.php?page=userinfo\">Edit Account</a>";
Assuming this will make your code include profile.php.
[PHP]include("profile.php"); [/PHP]
You should then have the session check in profile.php.
Eg:
[PHP]if (empty($session->username)) {
// die() or redirect header("Location: login.php") etc...
} else {
// now show the users profile
}[/PHP]
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
...I have a center column that on each click gets the variable $page and loads the correct page using an include("profile.php"); Now I need the link to look like this:
echo "<a href=\"index.php?page=userinfo?user=$session->username\">Edit Account</a>";
...
When you say the variable $page, I believe you really mean the variable $_GET['page']
When you have a url such asindex.php?page=userinfo, the varialbe $_GET['page'] will be set to the string: "userinfo" automatically by PHP in the file index.php.
If PHP has "register globals" enabled in its configuration, then the variable $page will also be set to "userinfo".
This is however disabled by default in later versions of PHP due to security risks in the way PHP code is written when "register globals" is enabled.
You should always write you code to retrieve HTTP varialbles from either the "$_GET array" or the "$_POST array".
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
UPDATE: SO i figured it out. I moved the statement you told me to put in before the include statement to the top of the userinfo.php :
$req_user = $_GET['username'] = $session->username;
And this seems to work. However I am concerned about security issues. Does anyone see anyproblems with what I am doing?
Thanks for all the help!
Try placing some debugging in your PHP code so you can check the value of your variables and what not.
eg:
[PHP]
$req_user = $_GET['username'] = $session->username;
echo "HTTP Username: {$_GET['username']}
";
echo "Session Username: {$session->username}
";
echo "\$reg_user: {$reg_user}
";
[/PHP]
or something like that.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101