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
<?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
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
<?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.
$username = $_GET['username'];This is the variable for the name that we pressed on in the member list page.
$user = mysql_query("SELECT * FROM user WHERE username = '$username'"); This variable will select all the info about that person.$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:
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 ->
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:
<?
$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.











