bookmark - How To Create A "user Profile" Page. No design (easy to add later if you want).

How To Create A "user Profile" Page. - No design (easy to add later if you want).

 
 Discussion by Feelay with 41 Replies.
 Last Update: June 7, 2012, 12:13 pm (View Latest)
Page 1 of 3 pages.
bookmark - How To Create A "user Profile" Page. No design (easy to add later if you want).  
    
free web hosting
 
Hi!

It was a long time ago I created a tutorial, so I've decided to create a new one :(

This time, I am going to teach you, how to create a "user profile page".
Lets say I am logged in on my account, and want to view someone else account information (in this case, only his username, but you can add more things
later).
Then I'll press on a link, that will take me to his user profile.
But before you can do that, you will have to create a register script, and a login script.

If you don't know how to create any of those, you can find tutorials here:
Login-Scipt Tutorial
Register-Script Tutorial

You will be using the same database, as the earlier tutorials.

Before I start, I just want to say that I would never have learned this, without some guys here on Astahost. So don't give me all the credits.
I think that vujsa, mastercomputers, Mordent and ethergeek, deserve some credits too. Because they teached me some of these things, that I am going to teach
you.




Lets start with the "Member List". To be able to see all the members that's registered on the page, with a link to their profile, we need a member list page.
Lets start with the code.


Members.php

CODE

<?php
session_start();
require 'database.php';
$nuser=$_SESSION['user'];
$auser=$_SESSION['admin'];

if($nuser){
$userfinal=$nuser;
}elseif($auser){
$userfinal=$auser;
}
if(isset($userfinal)){
$Members = mysql_query("SELECT user FROM characters WHERE level ='1' ORDER BY exp DESC") or die(mysql_error());
$numRowsMembers = mysql_num_rows($Members);
?>

<table border="0">

<?php
for($count = 1; $count <= $numRowsMembers; $count++)
{
$name = mysql_fetch_array($Members);
?>

<tr>
<?php
echo '<td><a href="member_profile.php?username=' . $name['user'] . '">' . $name['user'] . '</a></td>';
?>
</tr>

<?php
}
?>
</table>



So.. This is the Member List. I called it "members.php".
I think you want to know what it does, or?

The first thing we do, after the PHP tag, is starting a session. Not so hard..
Then we require the database file (I'll add it in the end of this tutorial).
Then, I create two variables.
The "$nuser" is for the "non-admin" user (normal user).
The "$auser" is for the "Admin" user.
After that I've created theese two variables, I check if the user is an Admin, or a Normal user, and insert the $nuser and $auser variable in the $userfinal

variable. now it is easy to check if the user is logged in. Instead of first checking if a Normal user is logged in, and then check if an admin is logged, I

check if both is logged in, much faster. (hard to explain :P but this way is much easier).
Now this explanation was for the "if(isset($userfinal)){" part.
After that, I create two new variables again.
The first one will get all the "Normal users" from the database.
The other one will retrieve the number of rows from the first query.

Then I create an Invisible Table, so that the names looks good, and so that the table don't screw up the design (if you use one.)
But OFC, if you want to make the table visible, you can change the "border" to a number larger than 0.

Now after that, we create a for-loop, to show all the names on the screen (the member list), and all the names, will have a link to their profile.




easy, wasn't it? If there is something you didn't understand, you are free to contact me and ask.
Now lets begin with the "member_profile" page.





member_profile.php


CODE

<?php
session_start();
require 'database.php';
$nuser=$_SESSION['user'];
$auser=$_SESSION['admin'];

if($nuser){
$userfinal=$nuser;
}elseif($auser){
$userfinal=$auser;
}
if(isset($userfinal)){

$username = $_GET['username'];
$user = mysql_query("SELECT * FROM user WHERE username = '$username'");
$user=mysql_fetch_assoc($user);
if($user['level'] > 1){
die("You cant view an Admins profile!");
}

echo "<h1>User Info</h1>";

echo "<b>Username:".$user['username']."<br>";

echo "<br>";
echo '<form name="backlistfrm" method="post" action="members.php">';
echo '<input type="submit" value="Back to The List">';
echo '</form>';
echo "<br>";

?>


Here we start like we did in the first page.
After the php tag, We start a session, and require the database file.
then we create two variables ($auser = admin and $nuser= normal user).
Then we create the $userfinal variable, to check if the user is logged in later in the script.
Then we create some variables.


CODE

$username = $_GET['username'];
This is the variable for the name that we pressed on in the member list page.

CODE

$user = mysql_query("SELECT * FROM user WHERE username = '$username'");
This variable will select all the info about that person.

CODE

$user=mysql_fetch_assoc($user);
This will make us able to select something specific about that person when we are writing the code.

Now we check if the user that we selected is a normal user, or an admin. If the user is an admin, an error will occur that the user can't view the admins

profile. If you want you can remove this part:



CODE

if($user['level'] > 1){
die("You cant view an Admins profile!");
}


This will make the users able to view an admins profile.

Then the info about the user will be shown.
In this case, I've only added the username of the user ->

CODE

echo "<b>Username:".$user['username']."<br>";


But OFC, you can add more. Just change the user inside the ['...'] to something else from the database table, that you want to show.

(lets say you want to show the users Admin level instead of his username, then you can change the $user['username'] to $user['level'].)

The last thing I do in this script, is adding a button that will take the user back the the member_list.




And here is the database.php file:

CODE

<?
$con = mysql_connect('localhost','mysql_username','mysql_password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('databasename');
?>


the first thing we do here, is:
Open a connection to the mysql server.
If the connection failed, the code will write an error
then, we select the database we want to use.



If you find anything spelled wrong, or any code that I wrote, that is wrong, or if you find something that is not working as it should, paste the wrong

word(s) or the wrong code(s) or the error(s), and I'll fix them.

Regards //Feelay




You are not allowed to copy anything from this tutorial, and paste it outside of this topic, without my permission.
But you are allowed to copy/paste parts of it, inside this topic.

Sun May 25, 2008    Reply    New Discussion   


Hello!
Thank you for the tutorials that you have made. It has helped me a lot with my website.
Just one question about this one; I only get "Parse error: syntax error, unexpected $end" in the members profile. It says line 35.
Do you think you could help me out ;)? Would be really nice.

+
Theres no such table as user.characters in your login/registration script either. Whats supposed to be in that one?
Thanks

Sat Sep 13, 2008    Reply    New Discussion   

Now this is my member_profile.php page. Is there something wrong with it as it wont show the username ?
The session name is username and the db file is db.php.

CODE

<?php
session_start();
require 'db.php';
$nuser=$_SESSION['username'];
$auser=$_SESSION['admin'];

if($nuser){
$userfinal=$nuser;
}elseif($auser){
$userfinal=$auser;
}
if(isset($userfinal)){

$username = $_GET['username'];
$user = mysql_query("SELECT * FROM user WHERE username = '$username'");
$user=mysql_fetch_assoc($user);
if($user['level'] > 1){
die("You cant view an Admins profile!");
}

echo "<h1>User Info</h1>";

echo "<b>Username:".$user['username']."<br>";

echo "<br>";
echo '<form name="backlistfrm" method="post" action="members.php">';
echo '<input type="submit" value="Back to The List">';
echo '</form>';
echo "<br>";

} else {
echo "You are not logged in. Please log in to continue";
}

?>

Mon Sep 15, 2008    Reply    New Discussion   

I'm trying this on my new website. For some reason if I go to the Members page it says "Table 'databaseName.charecters' doesn't exist". How can I fix it?

Wed Feb 24, 2010    Reply    New Discussion   


QUOTE (lysvir)

Hello!
Thank you for the tutorials that you have made. It has helped me a lot with my website.
Just one question about this one; I only get "Parse error: syntax error, unexpected $end" in the members profile. It says line 35.
Do you think you could help me out ;)? Would be really nice.
Link: view Post: 128256


add this, before the "?>".

CODE

} else {
echo "You are not logged in. Please log in to continue";
}


QUOTE (lysvir)

+
Theres no such table as user.characters in your login/registration script either. Whats supposed to be in that one?
Thanks
Link: view Post: 128256


I think I was using a different database when I created that tutorial.
just change the "user" to the column in your database that takes care of all usernames.
and change characters, to the name of the table you are using to store all the data.


Good luck with you site, and thanks for the comments ;)

Mon Sep 15, 2008    Reply    New Discussion   

Thanks! Now everything works perfect.
Can I just ask one more question?
Is it hard to make a script within this one, that let's the users upload a profile picture to the database?
I'm currently running everything on localhost with a phpmyadmin system.
I'm thinking something like a profile page that will show:

CODE

[PROFILE PICTURE]
- [PROFILE NAME]


That this will be shown to the user visiting the profile?
localhost/member_profile.php?username=Test
This will then show the username: TEST and the profile picture that TEST has uploaded.

Is this hard to make?

Wed Sep 17, 2008    Reply    New Discussion   

hmm.. I've never really studdied file upload. But it shoudln't be hard to make. You just have to know how to do it. I havn't studdied these things, and therefor, I'm afraid I can't help you =/
sorry. But I am sure that someone else here can.

EDIT: Try searching for UPLOAD PICTURES TO A MySQL DATABASE on Google ;) you should find lots of things.

Here is a good Link =)
Inserting pictures to a mysql database


Wishes //Feelay

Wed Sep 17, 2008    Reply    New Discussion   

Thanks for the fast reply, Feelay!
I will check it out ^^,

Wed Sep 17, 2008    Reply    New Discussion   

QUOTE (Feelay)

hmm.. I've never really studdied file upload. But it shoudln't be hard to make. You just have to know how to do it. I havn't studdied these things, and therefor, I'm afraid I can't help you =/
sorry. But I am sure that someone else here can.

EDIT: Try searching for UPLOAD PICTURES TO A MySQL DATABASE on Google ;) you should find lots of things.

Here is a good Link =)
Inserting pictures to a mysql database
Wishes //Feelay
Link: view Post: 128356



Just wondering with legal stuff and all, if any of us used this code on a commerical site would we get sued? or are we freely able to use it?

Thu Sep 18, 2008    Reply    New Discussion   

Hi again Feelay.
I'm working on a website that should contain text like this when logged in:
Welcome [username].
But my problem is, how can I make the user see their own profile page just by clicking their username here?
I tried this solution (which didn't work):

at the start:

CODE

<?php
session_start();
require_once 'db.php';
if (isset($_SESSION['username'])){
$user = $_SESSION['username'];
?>


Where I wanted the "Welcome [username]" without the "welcome" part:

CODE

<h4>Logged in</h4>
<fieldset>
<a href = "<?php echo "member_profile.php?username=$user"; ?>"><?php echo $user; ?></a>
<a href="logout.php">Log out</a>
</fieldset>


Do you know any working solution for my problem ;)?
Thanks.

Thu Sep 18, 2008    Reply    New Discussion   

QUOTE (bricktop11)

Just wondering with legal stuff and all, if any of us used this code on a commerical site would we get sued? or are we freely able to use it?
Link: view Post: 128389


The code is mine. I got some help from people here on astahost, with some functions. But the whole script is made by me. You are allowed to use it. This is just a beginner script :P So it doesn't really matter :P

QUOTE (lysvir)

Hi again Feelay.
I'm working on a website that should contain text like this when logged in:
Welcome [username].
But my problem is, how can I make the user see their own profile page just by clicking their username here?
I tried this solution (which didn't work):

at the start:

CODE

<?php
session_start();
require_once 'db.php';
if (isset($_SESSION['username'])){
$user = $_SESSION['username'];
?>


Where I wanted the "Welcome [username]" without the "welcome" part:

CODE

<h4>Logged in</h4>
<fieldset>
<a href = "<?php echo "member_profile.php?username=$user"; ?>"><?php echo $user; ?></a>
<a href="logout.php">Log out</a>
</fieldset>


Do you know any working solution for my problem ;)?
Thanks.
Link: view Post: 128391


hmm.. I don't understand =/

If you mean what you think you mean, try:

A simple if statement should be able to do it.
If the session user is set, show the welcome, else, show just the name of the user.


If that is not what you mean, please reask the question, but try to explain more ;)

Wishes //Feelay

Fri Sep 19, 2008    Reply    New Discussion   

I managed to figure it out by my self ^^,!
This is the code to the profile page:

CODE

<?php echo '<td><a href="member_profile.php?username=' . $_SESSION['username'] . '"> Your ingame stats/profile</td>'; ?>

Fri Sep 19, 2008    Reply    New Discussion   

thank you so much ... that was a big help...

Sun Sep 21, 2008    Reply    New Discussion   

Feelay, this script is great. I'm integrating it into a login/registration script I already have in place and I'm not sure if I'm doing something wrong. The members.php page gives me this error.
Parse error: parse error, unexpected $ in ..../members.php on line 34 (which is the end of the script and there is only a </table> tag, but no $ or other php info.

My table and fields are slightly different, but I haven't changed anything else. Any thoughts?

CODE

<?php
session_start();
require 'include/database.php';
$nuser=$_SESSION['user'];
$auser=$_SESSION['admin'];

if($nuser){
$userfinal=$nuser;
}elseif($auser){
$userfinal=$auser;
}
if(isset($userfinal)){
$Members = mysql_query("SELECT username FROM users WHERE userlevel ='1' ORDER BY exp DESC") or die(mysql_error());
$numRowsMembers = mysql_num_rows($Members);
?>
<table border="0">
<?php
for($count = 1; $count <= $numRowsMembers; $count++)
{
$name = mysql_fetch_array($Members);
?>
<tr>
<?php
echo '<td><a href="member_profile.php?username=' . $name['user'] . '">' . $name['user'] . '</a></td>';
?>
</tr>
<?php
}
?>
</table>

Sun Sep 28, 2008    Reply    New Discussion   

QUOTE (sozo33)

Feelay, this script is great. I'm integrating it into a login/registration script I already have in place and I'm not sure if I'm doing something wrong. The members.php page gives me this error.
Parse error: parse error, unexpected $ in ..../members.php on line 34 (which is the end of the script and there is only a </table> tag, but no $ or other php info.

My table and fields are slightly different, but I haven't changed anything else. Any thoughts?

CODE

<?php
session_start();
require 'include/database.php';
$nuser=$_SESSION['user'];
$auser=$_SESSION['admin'];

if($nuser){
$userfinal=$nuser;
}elseif($auser){
$userfinal=$auser;
}
if(isset($userfinal)){
$Members = mysql_query("SELECT username FROM users WHERE userlevel ='1' ORDER BY exp DESC") or die(mysql_error());
$numRowsMembers = mysql_num_rows($Members);
?>
<table border="0">
<?php
for($count = 1; $count <= $numRowsMembers; $count++)
{
$name = mysql_fetch_array($Members);
?>
<tr>
<?php
echo '<td><a href="member_profile.php?username=' . $name['user'] . '">' . $name['user'] . '</a></td>';
?>
</tr>
<?php
}
?>
</table>

Link: view Post: 128691

****UPDATED****
Okay, I realized that the bracket at if(isset($userfinal)){ has two closed brackets in the above code. Being a newbie, I'm not sure where to open another one, or eliminate one of the closed brackets so that they all cancel out. Maybe this will help anyone who looks at this. Thanks in advance!

Sun Sep 28, 2008    Reply    New Discussion   

Quickly Post to How To Create A "user Profile" Page. No design (easy to add later if you want). w/o signup Share Info about How To Create A "user Profile" Page. No design (easy to add later if you want). using Facebook, Twitter etc. email your friend about How To Create A "user Profile" Page. No design (easy to add later if you want). Print
Reply / Comment Ask a Question? Share / Bookmark E-Mail a Friend Print


Calendar And The Date () Function Making Math Simple With Modular Arithmetic  Calendar And The Date () Function Making Math Simple With Modular Arithmetic (0) (3) How To Find And Test Imagemagick Using Php   How To Find And Test Imagemagick Using Php