How To Let Users Visit Others Profile?

Ask about a PHP problem here.
Post Reply
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

How To Let Users Visit Others Profile?

Post by Shahlin »

I've seen Jacek's video...in which he teaches how to do that! but i don't know how to do it in my profile!
So, Can you tell me what are the codes and where to put it?

I want it to look somewhat like this... "profile.php?id=123" or somewhat similar!
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: How To Let Users Visit Others Profile?

Post by jacek »

You can use $_GET['id'] in the SQL to decided which users information to fetch ?
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

jacek wrote:You can use $_GET['id'] in the SQL to decided which users information to fetch ?


Sorry For The Late Reply!

But Where Should I Put $_GET ['id'] ?
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: How To Let Users Visit Others Profile?

Post by jacek »

It can be specified in the URL so somethign like profile.php?id=7

Then instead of querying the database based on the id stored in the session you would use the one from the url which is in $_GET['id']. I'm guessing what your code looks like but you essentially want to replace $_SESSION['id'] with $_GET['id'];
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

In My Login Form...The Code Looks Somewhat Like This :

[syntax=php]
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
[/syntax]

This Is Only For The Current User! If I Type profile.php?id=4 ...the same users profile is shown!
Where Should I Use The $_GET method and what should I put in profile.php?id="HERE" ?
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

Shahlin wrote:In My Login Form...The Code Looks Somewhat Like This :

[syntax=php]
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
[/syntax]

This Is Only For The Current User! If I Type profile.php?id=4 ...the same users profile is shown!
Where Should I Use The $_GET method and what should I put in profile.php?id="HERE" ?


GET is the method of extracting info from the URL. $_GET['id'] will be equal to whatever value you give ?ìd= in the URL.
if your url looks like this: profile.php?id=4 your $_GET['id'] will also be equal to 4.
So what you want to do is use whatever function you're using to find user information and pass in $_GET['id'], like this:
[syntax=php]<?php
fetch_user_info($_GET['id']);
?>[/syntax]

if you then pass in ?id=2 in the url, it will show the information for user with id 2.
[syntax=php]
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
[/syntax]
that code will always return the id of the currently logged in user and send the user to his or her own profile.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

I Tried This :

[syntax=php]
else if ($query_num_rows==1) {
$user_id = mysql_result ($query_run, 0, 'id') ;
$_SESSION ['user_id']=$user_id ;
$sql = mysql_query ("SELECT id FROM users");
$row = mysql_fetch_assoc ($sql) ;
$id = $row [$_GET['id']] ;
header ('Location: profile.php?id='.$id.'') ;
}
[/syntax]

But It Didn't Work!
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

that did not make sense. at all. sorry but I have no idea what you were trying to accomplish with that.
[syntax=php]
$id = $row [$_GET['id']] ; [/syntax]
what was the point of this?

Did you watch these tutorials?
http://www.youtube.com/playlist?list=PL074EAEF57B0DA2A4

It should give you the basic ideas of how to make this.

post all of your related code here and I'll try and explain things a bit better.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

This is my login form :

[syntax=php]<?php
include_once 'config.php' ;
require_once 'core.php' ;

if (loggedin()) { //loggedin is a function I created in core.php
header ('Location: access_denied.php') ;

} else {

if (isset($_POST['username'])&& isset($_POST['password'])) {
$username = $_POST ['username'] ;
$password = $_POST ['password'] ;
$password_hash = md5($password) ;

//login checks
if (!empty($username)&& !empty($password)) {

//query to check username & password
$query = "SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password_hash'" ;

if ($query_run = mysql_query ($query) ) {
$query_num_rows = mysql_num_rows ($query_run) ;

//query to fetch whether the account is activated or not! [0 or 1].
$sql = mysql_query ("SELECT `active` FROM `users`") ;
$fetch = mysql_fetch_assoc ($sql) ;
$active = $fetch ['active'] ;

//if no user found...
if ($query_num_rows==0) {
echo '<head><link rel="stylesheet" type="text/css" href="css/php.css"></head><p class="align">Invalid username/password combination.</p>' ;
//if a user is found...
} else if ($query_num_rows==1 && $active != 0) {

$user_id = mysql_result ($query_run, 0, 'id') ;
$_SESSION ['user_id']= $user_id ;
header ('Location: profile.php') ;
}
}
} else {
echo '<head><link rel="stylesheet" type="text/css" href="css/php.css"></head><p class="align">Please type in your username and password .</p>' ;
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Welcome To Budzzem</title>
<link rel="shortcut icon" href="css/Bdz.ico" />
<link rel="stylesheet" type="text/stylesheet" href="css/main.css">
<link rel="stylesheet" type="text/stylesheet" href="css/php.css">
<link rel="stylesheet" type="text/stylesheet" href="css/fadeBox.css">
</head>

<body>
<div id="whole_body">
<div id="dialog_box"><img src="css/Bdz.png" id="bdz"><span id="welcome_fade_box">Welcome to budzzem !<span><img src="css/close.png" id="close_fade_box"></span></span></div>
<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'> </script>
<script type="text/javascript">
$(document).ready(function() {
$('#dialog_box').fadeIn(1500);
});
$('#close_fade_box').click(function() {
$('#dialog_box').fadeOut(1500);
});
</script>
<header id="main_header"><div id="the_logo"><img src="css/Bdz.png" id="logo_pic"></div>
<div id="form_php"><form action="<?php echo $current_file ?>" method="POST">
<input type="text" placeholder="Username" name="username"><input type="password" placeholder="Password" name="password"> <input type="submit" name="login" value="Log in">
</form><a id="frgt_pass" href="forgot_pass.php">Forgot Password?</a></div>
<form action="register.php" method="POST" >
<p class="l1"><br>Username:</p><br><input type="text" name="username" class="l1" maxlength="30" placeholder="Username" value="<?php if (isset ($username)) { echo $username; } ?>"><br><br>
<p class="l1">Password:</p><br><input type="password" name="password" class="l1" placeholder="Password"><br><br>
<p class="l1">Confirm Password:</p><br><input type="password" name="confirm_password" class="l1" placeholder="Confirm Password"><br><br>
<p class="l1">E-mail:</p><br><input type="text" name="email" class="l1" placeholder="E-mail"><br><br>
<p class="l1">Firstname:</p><br><input type="text" name="firstname" maxlength="40" class="l1" placeholder="Firstname" value="<?php if (isset($firstname)){echo $firstname ;} ?>"><br><br>
<p class="l1">Surname:</p><br><input type="text" name="surname" class="l1" maxlength="40" placeholder="Surname" value="<?php if (isset($surname)) {echo $surname ;}?>"><br><br>
<p class="l1">Date of birth:</p><br><input type="text" name="dob" class="l1" maxlength="10" placeholder="Year-Month-Date" value="<?php if (isset($dob)) {echo $dob ; }?>"><br><br>
<input type="submit" value="Register">
</form>
</div>

</body>
</html>[/syntax]

My core.php file

[syntax=php]<?php
ob_start() ;
session_start () ;
$current_file = $_SERVER ['SCRIPT_NAME'] ;

function loggedin() {
if (isset($_SESSION['user_id'])&& !empty ($_SESSION['user_id'])) {
return true ;
} else {
return false ;
}
}

function getuserfield ($field) {
$query = "SELECT `$field` FROM `users` WHERE `id`='".$_SESSION['user_id']."'" ;
if ($query_run = mysql_query ($query)) {
if ($query_result = mysql_result ($query_run, 0, $field)) {
return $query_result ;
}
}
}
?>[/syntax]

And My Profile Page :
[syntax=php]<?php
require_once 'core.php' ;
require_once 'config.php' ;

if (isset($_SESSION['user_id'])&& !empty ($_SESSION['user_id'])) {
$user_id = $_SESSION ['user_id'] ;
$username = getuserfield ('username') ;
$firstname = getuserfield ('firstname') ;
$surname = getuserfield ('surname') ;
$dob = getuserfield ('dob') ;
$age = getuserfield ('age') ;
$status = getuserfield ('Status') ;
$image = getuserfield ('imagelocation') ;
} else {
header ('Location: loginform.php') ;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title><?php print $firstname.' '.$surname.'\'s Profile' ?></title>
<link rel="shortcut icon" href="css/Bdz.ico" />
<link rel="stylesheet" type="text/stylesheet" href="css/profile.css">
</head>

<body>
<div id="whole_body">
<div id="main_header"><p id="title">Budzzem<span id="links_for_user"><a href="logout.php"><span id="logout_link">Logout</span></a></span></p></div>

<div id="space">
<div id="aside">
<div id="prof_pic_holder">
<img src='<?php print $image ; ?>' width = '200' height = '110' id="img">
</div>
<div id="aside_info">
<table id="aside_table_info">
<tr><p class="info">Name: <span class="value"><?php print $firstname.' '.$surname ; ?></span></p></tr>
<tr><p class="info">Age: <span class="value"><?php print $age ;?></span></p></tr>
<tr><p class="info">Status: <span class="value"><?php print $status ;?></span></p></tr>
<tr><p class="info">Birthday: <span class="value"><?php print $dob ;?></span></p></tr>
<tr><p class="info"><a href="send_email.php">Send Email</a></p></tr>
<tr><p class="info"><a href="edit_profile.php">Edit profile</a></p></tr>
</table>
</div>

</div>
</div>
<footer id="main_footer">
<p id="footer">©Buddzem' 2012</p>
</footer>
</div>

</body>
</html>
[/syntax]

These Are The Pages Related!
And I'll Watch The Videos Again! To Get Info!
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

Alright. I see now.

Try doing this:
Add a second parameter to the getuserfield function, call it $id or something along those lines.
Change $_SESSION['user_id'] in your query to what you named the second parameter.

[syntax=php]function getuserfield ($field,$id) {
$query = "SELECT `$field` FROM `users` WHERE `id`='".$id."'" ;
if ($query_run = mysql_query ($query)) {
if ($query_result = mysql_result ($query_run, 0, $field)) {
return $query_result ;
}
}
}[/syntax]

And in your profile.php page, you add $_GET['id'] as the second parameter.
[syntax=php]if (isset($_SESSION['user_id'])&& !empty ($_SESSION['user_id'])) {
$user_id = $_GET['id'];
$username = getuserfield ('username',$user_id) ;
$firstname = getuserfield ('firstname',$user_id) ;
$surname = getuserfield ('surname',$user_id) ;
$dob = getuserfield ('dob',$user_id) ;
$age = getuserfield ('age',$user_id) ;
$status = getuserfield ('Status',$user_id) ;
$image = getuserfield ('imagelocation',$user_id) ;
}[/syntax]

that should work.

Although I do not recommend running one query per field. That will KILL your servers performance.
Run one query to get all fields where id = $_GET['id'] instead. One query per profile view is a hell of a lot more effective than 7 :)

I also recommend using echo instead of print. Print is a tad bit slower than echo ( not really noticeable, but still good practice to not use print ).
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

I Tried What You Said! But I Get This Error Repeatedly :

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 6 in C:\xampp\htdocs\php\budzzem\core.php on line 17

Warning: Missing argument 2 for getuserfield(), called in C:\xampp\htdocs\php\budzzem\profile.php on line 9 and defined in C:\xampp\htdocs\php\budzzem\core.php on line 14

Notice: Undefined variable: id in C:\xampp\htdocs\php\budzzem\core.php on line 15

And My Profile Page Info Field's Are Empty! I Guess It's Because I Removed The $_SESSION variable In The Query Of getuserfield (core.php)
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

post the code you have now.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

Oh I Am Sorry. I Fixed It...And It works Perfectly Now! :D
Only Thing Is...When I Go To profile.php The Fields Are Blank. No Info Is Shown...Is There A Fix?
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

Shahlin wrote:Oh I Am Sorry. I Fixed It...And It works Perfectly Now! :D
Only Thing Is...When I Go To profile.php The Fields Are Blank. No Info Is Shown...Is There A Fix?

i would need to see your code.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

Temor wrote:
Shahlin wrote:Oh I Am Sorry. I Fixed It...And It works Perfectly Now! :D
Only Thing Is...When I Go To profile.php The Fields Are Blank. No Info Is Shown...Is There A Fix?

i would need to see your code.


Profile.php

[syntax=php]<?php
require_once 'core.php' ;
require_once 'config.php' ;

if (isset($_SESSION['user_id'])&& !empty ($_SESSION['user_id'])) {

$user_id = $_GET ['id'] ;
$email = getuserfield ('email',$user_id) ;
$firstname = getuserfield ('firstname',$user_id) ;
$surname = getuserfield ('surname',$user_id) ;
$dob = getuserfield ('dob',$user_id) ;
$age = getuserfield ('age',$user_id) ;
$status = getuserfield ('Status',$user_id) ;
$image = getuserfield ('imagelocation',$user_id) ;
} else {
header ('Location: loginform.php') ;
}

?>[/syntax]

core.php

[syntax=php]<?php
ob_start() ;
session_start () ;
$current_file = $_SERVER ['SCRIPT_NAME'] ;

function loggedin() {
if (isset($_SESSION['user_id'])&& !empty ($_SESSION['user_id'])) {
return true ;
} else {
return false ;
}
}

function getuserfield ($field,$id) {
$query = "SELECT `$field` FROM `users` WHERE `id`='".$id."'" ;
if ($query_run = mysql_query ($query)) {
if ($query_result = mysql_result ($query_run, 0, $field)) {
return $query_result ;
}
}
}
?>
[/syntax]

Loginform.php

[syntax=php]
<?php
include_once 'config.php' ;
require_once 'core.php' ;

if (loggedin()) {
header ('Location: access_denied.php') ;
} else {
if (isset($_POST['username'])&& isset($_POST['password'])) {
$username = $_POST ['username'] ;
$password = $_POST ['password'] ;
$password_hash = md5($password) ;
if (!empty($username)&& !empty($password)) {
$query = "SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password_hash'" ;
if ($query_run = mysql_query ($query) ) {
$query_num_rows = mysql_num_rows ($query_run) ;
if ($query_num_rows==0) {
echo '<head><link rel="stylesheet" type="text/css" href="css/php.css"></head><p class="align">Invalid username/password combination.</p>' ;
} else if ($query_num_rows==1) {
$user_id = mysql_result ($query_run, 0, 'id') ;
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
}
}

} else {
echo '<head><link rel="stylesheet" type="text/css" href="css/php.css"></head><p class="align">Please type in your username and password .</p>' ;
}
}
}
?>
[/syntax]
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

what does the URL look like when you go to profile.php?

are you including the ?id= ?

your url should look like this:
/profile.php?id=x

x is the number corresponding to the ID in the database.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

Temor wrote:what does the URL look like when you go to profile.php?

are you including the ?id= ?

your url should look like this:
/profile.php?id=x

x is the number corresponding to the ID in the database.


When I First Login...It Takes Me To The Profile...With The User's Id In The URL! Like : ../profile.php?id=4 .
After That When I Go To ../profile.php . Everything's Blank!
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

Shahlin wrote:
Temor wrote:what does the URL look like when you go to profile.php?

are you including the ?id= ?

your url should look like this:
/profile.php?id=x

x is the number corresponding to the ID in the database.


When I First Login...It Takes Me To The Profile...With The User's Id In The URL! Like : ../profile.php?id=4 .
After That When I Go To ../profile.php . Everything's Blank!


you need to have the ?id part in the url for it to show any info. The ?id= part defines which users info to show. If it's not there, the script does not know what info to show. If you want it to show the logged in users information by default you could do that with an if statement.

[syntax=php]if(empty($_GET['id'])){
$user_id = $_SESSION['user_id'];
}else{
$user_id = $_GET['id'];
}[/syntax]
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: How To Let Users Visit Others Profile?

Post by Shahlin »

Thanks ALOT! :D
Everything Works! :D
Thanks Again! :)
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How To Let Users Visit Others Profile?

Post by Temor »

Glad I could help.
Post Reply