13 Replies - 9411 Views - Last Post: 13 October 2012 - 06:02 PM Rate Topic: -----

#1 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

PHP User Profile View

Posted 12 October 2012 - 11:33 PM

I am looking for a way to create a view profile page. I have registration and logging in good. Now I'm trying to get a way for users to view other users profiles. I know how to have a user view their own, but say user a wants to see user b's info. How would I accomplish this? I don't even know where to start on this since it's a dynamic link and I'm lost, so sorry for the question, but any help would be appreciated. Thanks again.
Is This A Good Question/Topic? 0
  • +

Replies To: PHP User Profile View

#2 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 244
  • View blog
  • Posts: 715
  • Joined: 24-November 08

Re: PHP User Profile View

Posted 13 October 2012 - 05:19 AM

Easiest way is probably to use a $_GET link. Say you have a link like: <a href='profile.php?user=bob' and a profile.php is a template page that gets it's information based on the $_GET['user'] variable.

When using this method, it's important to validate the URL, in order to stop someone manually trying to find a user who doesn't exist, or worse, trying to sql inject your site.

<?php
session_start();
$verify = verifyUser($_GET['user']);

if(!$verify)
{
    //user does not exist or you dont have permission to view their profile
    //navigate away from profile.php
    $_SESSION['errorMsg'] = "Either you do not have permission to access this profile or this profile does not exist";
    header("location: error.php"); // where you can display the error message
    exit(); //stop the rest of your page from executing
}

//user verified
// rest of your profile code here

?>



This is only a very basic example. The verifyUser() function would need to sanitize the user input and check the database for if the user exists and any other relation you may want to check (friends etc).

This can be done without a $_GET array, but the reason I use this on my site is so that people can link others to their profile (or a friends).

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#3 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 08:36 AM

I think I've worded my question wrong. I have the link to go to view the page. ((However I had nothing verifying the user, so thanks for that. :) )) But the main problem I have is I'm at a lost as to how to make the view profile link, if you go to view profile it's going to need to pull the information from the link that you have and the way I'm set up right now is
<?php echo $admin->get_yourname(); ?> 

So I know that I'd need a link to get to that profile depending on what they click they link will need to pull from the id or username. Or at least that's what I would assume.
<a href="view_profile.php"><?php echo $admin->get_yourname(); ?>

Now isn't there a way for the view profile link to actually pull depending on the $admin->get_yourname portion and pull relevant information? Sorry if it's an easy question to answer, but I'm stumped at this point.
Was This Post Helpful? 0
  • +
  • -

#4 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 244
  • View blog
  • Posts: 715
  • Joined: 24-November 08

Re: PHP User Profile View

Posted 13 October 2012 - 01:02 PM

So you want the information from your $admin->get_yourname(); function to populate your link?

<?php

$link = "<a href='view_profile.php?user=" . $admin->get_yourname() . "'>" . $admin->get_yourname() . "</a>";

echo $link;

?>



This will print <a href='view_profile.php?user=returned_username'>returned_username</a>
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: PHP User Profile View

Posted 13 October 2012 - 01:33 PM

One thing to note about your first code, Slice. You don't actually verify that the $_GET value exists there before you use it, which means that a PHP notice will be issued if it isn't. You'd be better of doing something along the lines of:
<?php
if (empty($_GET["user"]) || !validateUserName($_GET["user"])) {
    header("HTTP/1.0 404 Not Found");
    include "userNotFoundError.html";
    exit;
} 
else {
    // Show user info.
}


Not redirecting to show the error will also save you a bit of bandwidth. There is really no reason not to show the error on the same page. In fact, it'll be easier for the user to navigate back if you do.


By the way, I moved this to the PHP forum. Questions about PHP specific things are better put here than in the generic Web Development forum.
Was This Post Helpful? 2
  • +
  • -

#6 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 02:34 PM

@Slice. Kind of, this displays a link, but since I'm using $admin->get_nicename the $admin->get function I have defined is based off of a session variable, so only shows a link to you, however it's a start; it gets the link I was looking for, but I'm looking more along the lines of
 $result=mysql_query("SELECT * FROM user WHERE permissions <='1'");
			while ($row=
			mysql_fetch_array($result))
				{
				echo '<br>';
				echo $row['nicename'];
				echo '<br>';
  				}

With the code
$link = "<a href='view_profile.php?user=" . $admin->get_nicename() . "'>" . $admin->get_nicename() . "</a>";	

It will only display the link of the logged in user, so I'm trying to find a way to where it finds the nicename IE the display name and then displays all of those names therefore creating a userlist. When you click on the registered user it will then take you to the view_profile.php link and display the information on them;
$link = "<a href="view_profile.php?user=" . $_GET['nicename'] . "'>" $row['nicename'] . "</a>
is going to be my next attempt, but I don't think that will work since the _GET function won't know what it's looking for. (Or my code is going to be smarter than me.)
Was This Post Helpful? 0
  • +
  • -

#7 MrXtremeM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 02:43 PM

Well, best method would be making a php script to handle the view profile and passing the user id using get parameters.
This is the best and the easiest way to do it.
Fetch the record from database using the id and print them the way u want..
Based on the user id, u can also show the activity of the user in his view profile..(that depends on the site you are building.)
Was This Post Helpful? 0
  • +
  • -

#8 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 244
  • View blog
  • Posts: 715
  • Joined: 24-November 08

Re: PHP User Profile View

Posted 13 October 2012 - 02:51 PM

The $_GET array is formed from your link. So the bit after your page with the '?' and then you set the variable ?user=.

If you were going to make links for your members from a database then your code isn't that far off. You just need to use your while loop to make the links.

example:
$result=mysql_query("SELECT * FROM user WHERE permissions <='1'");

while ($row=mysql_fetch_array($result))
{
	$link = "<a href='view_profile.php?user=" . $row['nicename'] . "'>" . $row['nicename'] . "</a>";
    echo $link . "<br/>";
}



Then on your view_profile.php use the $_GET['user'] to pull all that users specific information.

I don't know how big this project is but it will cause a strain on your database performance if you have several users. In your query, try to avoid using the * as it retrieves a lot of information that you probably don't need.

mysql_query("SELECT nicename FROM user WHERE permissions <='1'");



Look into prepared statements and try to avoid using the old mysql_ functions. They are depreciated and insecure. PDO or MySqli would be suitable alternatives :)
Was This Post Helpful? 0
  • +
  • -

#9 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 03:17 PM

Thanks for the help. :) I started really hardcore toying with php and mysql around a month ago. I've been slowly converting all my mysql functions to mysqli, I've noticed also that I have a MUCH easier time with the mysqli function after I have a little knowledge with the mysql function; however I don't make that exception with user input data; that's all done using mysqli prepared escape strings. the project is very small at the moment; kind of a self homework thing since everything I do is self taught through forums and people willing to help, but before I really get it up and going I do plan on doing a thorough cleanup. IE I see the connection going 7-8 times throughout the page. Much more effective to start the creation before the header and close at the end. I've tried that on a few pages and seen a nice increase in performance. :)
Was This Post Helpful? 0
  • +
  • -

#10 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 03:36 PM

So last question on this then I should be good to go (I think).
From Atli:
<?php
if (empty($_GET["user"]) || !validateUserName($_GET["user"])) {
    header("HTTP/1.0 404 Not Found");
    include "userNotFoundError.html";
    exit;
} 
else {
    // Show user info.
}


So instead of using the $_GET["user]) I would use $_GET["nicename"] and then just on the else section
I would then use $echo [sqltablehere]? I just want to clarify because it doesn't seem to be working.
Was This Post Helpful? 0
  • +
  • -

#11 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 244
  • View blog
  • Posts: 715
  • Joined: 24-November 08

Re: PHP User Profile View

Posted 13 October 2012 - 03:49 PM

You would only be using $_GET['nicename'] if the link has a nicename variable. All $_GET does is read variables in from the URL.

Here is the manual page for get.

If you post all of your current code we would have a much better overview of any problems.
Was This Post Helpful? 0
  • +
  • -

#12 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 03:55 PM

I'll take a look through the get manual page and if I can't figure it out then I'll post my current code and if I figure it out then I'll post my working page. Thanks a ton for your help.
Was This Post Helpful? 0
  • +
  • -

#13 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: PHP User Profile View

Posted 13 October 2012 - 04:02 PM

I'm using your get function from earlier to try to receive the info from earlier and it's standing at.
<?php
require '../config.php';
$con;
session_start();
$verify = verifyUser($_GET['nicename']);

if(!$verify)
{
    //user does not exist or you dont have permission to view their profile
    //navigate away from profile.php
    $_SESSION['errorMsg'] = "Either you do not have permission to access this profile or this profile does not exist";
    header("location: error.php"); // where you can display the error message
    exit(); //stop the rest of your page from executing
}
//user verified
// rest of your profile code here

echo $row['nicename'];

?>

The nicename row isn't displaying so I'm trying to find through the manual now how to accomplish what I'm trying to do exactly.
Was This Post Helpful? 0
  • +
  • -

#14 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP User Profile View

Posted 13 October 2012 - 06:02 PM

Hi,

Is there a $row variable defined anywhere?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1