Page 1 of 1

How To Let Users Visit Others Profile?

Posted: Thu Jan 26, 2012 5:55 am
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!

Re: How To Let Users Visit Others Profile?

Posted: Sat Jan 28, 2012 10:45 pm
by jacek
You can use $_GET['id'] in the SQL to decided which users information to fetch ?

Re: How To Let Users Visit Others Profile?

Posted: Thu Feb 02, 2012 11:48 am
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'] ?

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 1:05 am
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'];

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 2:01 pm
by Shahlin
In My Login Form...The Code Looks Somewhat Like This :
	
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
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" ?

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 4:07 pm
by Temor
Shahlin wrote:In My Login Form...The Code Looks Somewhat Like This :
	
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
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:
<?php
fetch_user_info($_GET['id']);
?>
if you then pass in ?id=2 in the url, it will show the information for user with id 2.
	
$_SESSION ['user_id']=$user_id ;
header ('Location: profile.php?id='.$user_id.'') ;
that code will always return the id of the currently logged in user and send the user to his or her own profile.

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 4:29 pm
by Shahlin
I Tried This :
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.'') ;
		}
But It Didn't Work!

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 5:58 pm
by Temor
that did not make sense. at all. sorry but I have no idea what you were trying to accomplish with that.
$id = $row [$_GET['id']] ; 
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.

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 6:16 pm
by Shahlin
This is my login form :
<?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/jq ... ery.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>
My core.php file
<?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 ;
		}
	}
}
?>
And My Profile Page :
<?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>
These Are The Pages Related!
And I'll Watch The Videos Again! To Get Info!

Re: How To Let Users Visit Others Profile?

Posted: Fri Feb 03, 2012 7:31 pm
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.
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 ;
                }
        }
}
And in your profile.php page, you add $_GET['id'] as the second parameter.
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) ;
}
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 ).

Re: How To Let Users Visit Others Profile?

Posted: Sat Feb 04, 2012 12:42 pm
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)

Re: How To Let Users Visit Others Profile?

Posted: Sat Feb 04, 2012 3:11 pm
by Temor
post the code you have now.

Re: How To Let Users Visit Others Profile?

Posted: Sat Feb 04, 2012 7:04 pm
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?

Re: How To Let Users Visit Others Profile?

Posted: Sat Feb 04, 2012 10:44 pm
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.

Re: How To Let Users Visit Others Profile?

Posted: Sun Feb 05, 2012 1:02 pm
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
<?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') ;
}

?>
core.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 ;
		}
	}
}
?>
Loginform.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>' ; 
}
}
}
?>

Re: How To Let Users Visit Others Profile?

Posted: Sun Feb 05, 2012 2:23 pm
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.

Re: How To Let Users Visit Others Profile?

Posted: Sun Feb 05, 2012 5:57 pm
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!

Re: How To Let Users Visit Others Profile?

Posted: Sun Feb 05, 2012 8:18 pm
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.
if(empty($_GET['id'])){
$user_id = $_SESSION['user_id'];
}else{
$user_id = $_GET['id'];
}

Re: How To Let Users Visit Others Profile?

Posted: Mon Feb 06, 2012 11:50 am
by Shahlin
Thanks ALOT! :D
Everything Works! :D
Thanks Again! :)

Re: How To Let Users Visit Others Profile?

Posted: Mon Feb 06, 2012 12:32 pm
by Temor
Glad I could help.