Session Uid in A href

Ask about a PHP problem here.
Post Reply
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Session Uid in A href

Post by TehCrayz »

Hello, i havn'nt used php for a while because i gave up. But now im back to give it another try...So i fail straight away and need a solution to this.

I have been following the profile tutorial and have a profile on uid 1. But i don't know to be able to make that when i click on profile, it will take me to Profiles/profile.php?uid=""
$SESSION['uid'']???
[syntax=php] <li><a href=""><i class="icon-user"></i>Profile</a></li>
<li><a href="#"><i class="icon-cog"></i>Settings</a></li>
<li><a href="Login/logout.php"><i class="icon-remove"></i>Log out</a></li>[/syntax]
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Session Uid in A href

Post by Temor »

$_SESSION['uid'] will always be the ID of the currently logged in user, so using that in your url will be the equivalent of a "My Profile" link. I.E it will always take you to your own profile.

There are a multitude of ways to get an id into the url, but I'm gonna need more info on what you're trying to do exactly to be able to help with that :)
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Session Uid in A href

Post by TehCrayz »

Basically, on the main page, it says the users name, they click it. It's a drop down, and it says "Profile", and so when they click it, it takes them to their profile. Simple.

<a href="*insert thingy here*"><i class="icon-user"></i>Profile</a></li>

I just don't know how to add it in. :mrgreen:
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Session Uid in A href

Post by Temor »

Oh, that would be really simple :P

[syntax=php]
<a href="Profiles/profile.php?uid=<?php echo $_SESSION['uid'];?>"><i class="icon-user"></i>Profile</a></li>
[/syntax]

If you have the users ID stored in $_SESSION['ui'd], and your profile.php page is located in Profiles/, then this will work.
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Session Uid in A href

Post by TehCrayz »

[EDIT] Or, if it doesn't do what's below, it just takes me to uid=1


When i change it to that, i get this in the URL:

http://localhost/Quodec/Profiles/profil ... 20%3Cb%3EC:\xampp\htdocs\Quodec\pictures.php%3C/b%3E%20on%20line%20%3Cb%3E417%3C/b%3E%3Cbr%20/%3E

...I don't get it :/
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Session Uid in A href

Post by Temor »

That's because you don't have a value in $_SESSION['uid'].

Where are you storing the logged in user ID?
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Session Uid in A href

Post by TehCrayz »

What do you mean by that?

[syntax=php]<?php

function user_exists($user){
$user = mysql_real_escape_string($user);

$total = mysql_query("SELECT COUNT(`ID`) FROM `users` WHERE `Username` = '{$user}'");

return (mysql_result($total, 0) == '1') ? true : false;
}

function valid_credentials($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = sha1($pass);

$total = mysql_query("SELECT COUNT(`ID`) FROM `users` WHERE `Username` = '{$user}' AND `Password` = '{$pass}'") or die(mysql_error());

return (mysql_result($total, 0) == '1') ? true : false;
}


function add_user($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = sha1($pass);

mysql_query("INSERT INTO `users` (`Username`, `Password`) VALUES ('{$user}', '{$pass}')");
}

?>[/syntax]
??
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Session Uid in A href

Post by Temor »

Well, when you log a user in, you set some values in $_SESSION, do you not?
You need to grab the users ID from your Database and insert it into $_SESSION['uid'], otherwise you have no value in $_SESSION['uid'], and trying to grab a value from a variable with no value isn't the easiest thing to do.
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Session Uid in A href

Post by TehCrayz »

Ah, so you're trying to say that right now $_SESSION['uid'] isn't getting the IDs from the database??

...


I'm confused... :?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Session Uid in A href

Post by Temor »

Yeah, you're never assigning a value to $_SESSION['uid'].
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Session Uid in A href

Post by TehCrayz »

So uh, How can i fix this issue? :)

Sorry, i'm kinda a noob at PHP.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Session Uid in A href

Post by Temor »

Follow the tutorial again :P It's in there, you just missed it I guess.

It's the login part. You need to set the $_SESSION variable to "log in" the user.
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Session Uid in A href

Post by TehCrayz »

Ok, i'll look back on the tutorial. Thanks. :D
Post Reply