Display user profile with image in php

Display user’s profile with image on search in php

Hi there after a long time,  i am back with a tutorial on User Image display on search in php .  In this tutorial i will discuss about some more security features in relation of my previous post on  Creating user profile Page using PHP and MYSQL .

You can check out demo of this tutorial by clicking button below:

View Demo                      Download Zip

In my previous post i used db.php to establish connection. But this time i have created “function.php” which will include connection strings and add some important functions.

function.php

<?php $con=mysqli_connect("localhost","root",""); if($con) { echo "connected"; } else { echo "not connected"; } mysqli_select_db($con,"database name"); function session_value($val){ if($val=="id"){ $res=$_SESSION['user_id']; }elseif($val=="username"){ $res=$_SESSION['username']; }elseif($val=="login"){ $res=$_SESSION['login_user']; } return $res; } function user_check($page){ if($page=="home"){ if(length(session_value("login"))!='1'){ header("location: welcome.php"); } }if($page==""){ $ss=length(session_value("login")); if(length(session_value("login"))=='1'){ header("location: index.php?remark_login=failed"); } } } function user_session(){ global $con,$prefix; session_start(); if(isset($_SESSION['user_id']) && $_SESSION['user_id'] != ""){}else{ $x=substr(str_shuffle('23456789'),0,4); $_SESSION['user_id']=$x; } if(isset($_SESSION['username']) && $_SESSION['username'] != ""){}else{ $x=substr(str_shuffle('ABCDEFRTOPICS23456789'),0,5); $_SESSION['username']="Guest_".$x.""; } if(isset($_SESSION['login_user']) && $_SESSION['login_user'] != ""){}else{ $x=substr(str_shuffle('abcdefrtopics23456789'),0,1); $_SESSION['login_user']=$x; } } function length($get){ $x=strlen($get); return $x; } function user_info($id,$value="name"){ global $con; $rs=""; $sql=mysqli_query($con,"Select * from demo3_member where mem_id='$id'"); $row=mysqli_fetch_array($sql); if($value=="name"){ $rs=$row['name']; }if($value=="username"){ $rs=$row['username']; }if($value=="email"){ $rs=$row['email']; }if($value=="dob"){ $rs=$row['dob']; }if($value=="profession"){ $rs=$row['profession']; }if($value=="gender"){ $rs=$row['gender']; }if($value=="location"){ $rs=$row['location']; } if($value=="image_check"){ $rs=$row['image_url']; } if($value=="image"){ if($row['image_url']!=""){ $rs=$row['image_url']; }else{ $rs="upload/user.png"; } }if($value=="image_url"){ if($row['image_url']!=""){ $rs="upload/pic_".$row['image_url']; }else{ $rs="upload/user.png"; } }if($value=="image_thumb"){ if($row['image_url']!=""){ $rs="upload/thumb_".$row['image_url']; }else{ $rs="upload/user.png"; } } if($value=="website"){ $rs=$row['website']; }if($value=="about_me"){ $rs=$row['about_me']; } return $rs; } ?>

Now we will create table for members named as member. To create table simply import contents of table.sql or directly run tqble query in sql query window of phpmyadmin .

table.sql

CREATE TABLE IF NOT EXISTS `member` (
`mem_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(120) NOT NULL,
`email` varchar(150) NOT NULL,
`name` varchar(50) NOT NULL,
`dob` varchar(21) NOT NULL,
`profession` varchar(120) NOT NULL,
`gender` varchar(20) NOT NULL,
`location` text NOT NULL,
`image_url` varchar(250) NOT NULL,
`website` varchar(150) NOT NULL,
`about_me` text NOT NULL,
PRIMARY KEY (`mem_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Now we will create a login and registration form. As in previous tutorial i already mentioned script for login and registration form. I am providing login and registration form code below. Simply copy it to your index page.

index.php

<?php include "function.php"; user_session(); user_check("home"); ?>





<div class="tp-contentwrap2">





<div>Login Form</div>









<div class="" id="t1">
<?php
$remarks = isset($_GET['remark_login']) ? $_GET['remark_login'] : '';
if ($remarks==null and $remarks=="")
{
echo '<span class="tp-regtext eee">Login Here</span>';
}
if ($remarks=='field_empty')
{
echo '<script>window.onload=tabtoggle("t1");</script>';
echo '<span class="tp-regtext lightred">Fill all the fields.</span>';
}
if ($remarks=='failed')
{
echo '<script>window.onload=tabtoggle("t1");</script>';
echo '<span class="tp-regtext lightred">Login Failed!, Invalid Credentials</span>';
}
?>





<form action="logincheck.php" method="post" >

<label for="username">Username: </label><input class="right" type="text" name="username" placeholder="Username"/>

<label for="password">Password: </label><input class="right" type="password" name="password" placeholder="password"/>

<button class="tp-btn create pointer" type="button" onclick='tabtoggle("t2");'>Create an account</button>

<center><input type="submit" name="submit" class="tp-btn submit pointer" value="Login"></center>
</form>





</div>









<div>Signup Form</div>









<div class="" id="t2">
<?php
$remarks = isset($_GET['remarks']) ? $_GET['remarks'] : '';
if ($remarks==null and $remarks=="")
{
echo '<span class="tp-regtext eee">New User! Register Here</span>';
}
if ($remarks=='success')
{
echo '<script>window.onload=tabtoggle("t2");</script>';
echo '<span class="tp-regtext green">Registration Success</span>';
}
if ($remarks=='failed')
{
echo '<script>window.onload=tabtoggle("t2");</script>';

echo '<span class="tp-regtext lightred">Registration Failed!, Username exists</span>';
}if ($remarks=='password_mismatch')
{
echo '<script>window.onload=tabtoggle("t2");</script>';

echo '<span class="tp-regtext lightred">Both Password must be same</span>';
}if ($remarks=='field_empty')
{
echo '<script>window.onload=tabtoggle("t2");</script>';

echo '<span class="tp-regtext lightred">Fill all the Fields.</span>';
}if ($remarks=='invalid_email')
{
echo '<script>window.onload=tabtoggle("t2");</script>';

echo '<span class="tp-regtext lightred">Invalid Email Address.</span>';
}
?>





<form name="reg" action="execute.php" method="post" id="reg">

<label for="name">Name: </label><input class="right" type="text" name="name" placeholder="Name"/>

<label for="username">Username: </label><input class="right" type="text" name="username" placeholder="Username"/>

<label for="password">Password: </label><input class="right" type="password" name="password" placeholder="password"/>

<label for="password">Repeat Password: </label><input class="right" type="password" name="passwordrepeat" placeholder="Retype password"/>

<label for="email">Email: </label><input class="right" type="text" name="email" placeholder="[email protected]"/>

<center><input type="submit" name="submit" class="tp-btn submit pointer" value="Register Me"/></center>
</form>





</div>





logincheck.php

<?php
include "function.php";
session_start();
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=md5($_POST['password']);
$result = mysql_query("SELECT * FROM member where username='$username' and password='$password'");
$c_rows = mysql_num_rows($result);

if ($c_rows<1) { header("location: index.php?remark_login=failed"); } if($username=="" || $password==""){ header("location: index.php?remark_login=field_empty"); } $sql="SELECT * FROM member WHERE username='$username' and password='$password'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); $count=mysql_num_rows($result); if($count==1) { if(isset($_SESSION['user_id']) && $_SESSION['user_id'] != ""){ $y=$row['mem_id']; $_SESSION['user_id']=$row['mem_id']; }else{ $_SESSION['user_id']=$row['mem_id']; } if(isset($_SESSION['username']) && $_SESSION['username'] != ""){ $x=$row['username']; $_SESSION['username']=$row['username']; } else{ $_SESSION['username']=$row['username']; } if(isset($_SESSION['login_user']) && $_SESSION['login_user'] != ""){ $x=substr(str_shuffle('abcdefrtopics23456789'),0,3); $_SESSION['login_user']=$x; }else{ $x=substr(str_shuffle('abcdefrtopics23456789'),0,3); $_SESSION['login_user']=$x; } if(isset($_SESSION['time']) && $_SESSION['time'] != ""){}else{ $_SESSION['time']=time(); } header("location: ./welcome.php?remark_login=login_success"); } }else{ header("location: ./index.php?remark_login=access_denied"); } ?>

logout.php





<div id="code">




<div id="code-st"><?php session_start(); if(session_destroy()) { header("Location: index.php"); } ?></div>




</div>





execute.php

<?php session_start(); include('function.php'); $name=$_POST['name']; $email=$_POST['email']; $username=$_POST['username']; $password=md5($_POST['password']); $passwordrepeat=md5($_POST['passwordrepeat']); $result = mysql_query("SELECT * FROM member WHERE username='$username'"); $num_rows = mysql_num_rows($result); if($name=="" ||$email=="" ||$username=="" ||$password=="" ||$passwordrepeat=="" ){ header("location: index.php?remarks=field_empty"); }elseif($password!=$passwordrepeat) { header("location: index.php?remarks=password_mismatch"); }elseif($num_rows){ header("location: index.php?remarks=failed"); }elseif(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { header("location: index.php?remarks=invalid_email"); }else { mysql_query("INSERT INTO member(name, email,username, password)VALUES('$name', '$email','$username', '$password')"); header("location: index.php?remarks=success"); } ?>

Now we will go with Welcome page that any logged in user will visit after login.

welcome.php

<?php include('function.php'); user_session(); user_check(""); ?>





<div class="tp-content">





<div class="">





<div class="navigator left">





<ul>





<li><a href="index.php">HOME</a></li>





<span> &raquo; </span>





<li>Display user image on search in php.</li>





</ul>





</div>





</div>









<div class="wt_99 center left">





<div class="right search_form">





<form action="search.php" method="post">
<input type="text" name="user" placeholder="Search.."/>
<input type="submit" name="submit" value="Search"/>
</form>





</div>





</div>









<div class="tp-contentbox">





<div class="tp-infostrip">





<nav>





<ul>





<li class="left"><a href="welcome.php">Profile</a></li>









<li class="left"><a href="logout.php">Logout</a></li>









<li class="right"><a href="welcome.php">Hello <?php echo session_value("username"); ?></a></li>









<ul>
</nav>





</div>









<div class="tp-contentwrap1">





<div class="left wt_25 side_1">





<div class="image_block">
<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
</div>









<div class="menu_list">





<li><a href="edit_profile.php">Edit Profile</a></li>









<li><a href="deleteac.php">Delete Account</a></li>





</div>





</div>









<div class="right wt_75">
<?php $id=session_value("id"); $sql="SELECT * FROM member where mem_id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?>





<div class="tp-contentwrap2">





<div class="strip-profile">General Info</div>









<table>





<tr>





<td class="td_1">Username :</td>









<td class="left"><?php echo $rows['username']; ?></td>





</tr>









<tr>





<td class="td_1">Name :</td>









<td class="left"><?php echo $rows['name']; ?></td>





</tr>









<tr>





<td class="td_1">Email id :</td>









<td class="left"><?php echo $rows['email']; ?></td>





</tr>









<tr>





<td class="td_1">Date Of Birth :</td>









<td class="left"><?php echo $rows['dob']; ?></td>





</tr>









<tr>





<td class="td_1">Gender :</td>









<td class="left"><?php echo $rows['gender']; ?></td>





</tr>









<tr>





<td class="td_1">Profession :</td>









<td class="left"><?php echo $rows['profession']; ?></td>





</tr>









<tr>





<td class="td_1">Location :</td>









<td class="left"><?php echo $rows['location']; ?></td>





</tr>









<tr>





<td class="td_1">Website :</td>









<td class="left"><?php echo $rows['website']; ?></td>





</tr>





</table>









<div class="strip-profile">About me</div>









<div class="wt_99 left">
<span class="left about"><?php echo $rows['about_me']; ?></span>
</div>





</div>





</div>





</div>





</div>





</div>





We will now create page to help user edit their profile. Copy and paste code given below:

edit_profile.php

<?php include('function.php'); user_session(); user_check(""); ?>





<div class="tp-content">





<div class="wt_99 center">





<div class="navigator left">





<ul>





<li><a href="index.php">HOME</a></li>





<span> &raquo; </span>





<li>Display user image on search in php.</li>





</ul>





</div>





</div>









<div class="wt_99 center left">





<div class="right search_form">





<form action="search.php" method="post">
<input type="text" name="user" placeholder="Search.."/>
<input type="submit" name="submit" value="Search"/>
</form>





</div>





</div>









<div class="tp-contentbox">





<div class="tp-infostrip">





<nav>





<ul>





<li class="left"><a href="welcome.php">Profile</a></li>









<li class="left"><a href="logout.php">Logout</a></li>









<li class="right"><a href="welcome.php">Hello <?php echo session_value("username"); ?></a></li>









<ul>
</nav>





</div>









<div class="tp-contentwrap1">





<div class="left wt_25 side_1">





<div class="image_block">
<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
</div>









<div class="menu_list">





<li><a href="edit_profile.php">Edit Profile</a></li>









<li><a href="deleteac.php">Delete Account</a></li>





</div>





</div>









<div class="right wt_75">
<?php $var=session_value("id"); $sql="SELECT * FROM member where mem_id=$var"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?>





<div class="tp-contentwrap2">





<div class="strip-profile">Edit Profile</div>





<?php
$remarks = isset($_GET['remark_login']) ? $_GET['remark_login'] : '';
if ($remarks==null and $remarks=="")
{}
if ($remarks=='invalid_email')
{
echo '<span class="tp-regtext lightred">Invalid E-mail.</span>';
}if ($remarks=='name')
{
echo '<span class="tp-regtext lightred">Enter Your Name.</span>';
}
?>





<form action="updateac.php" method="post" enctype="multipart/form-data">





<table>





<tr>





<td class="td_1">Image :</td>









<td class="right"><input type="file" name="image_file"/></td>





</tr>









<tr>





<td class="td_1">Name :</td>









<td class="right"><input type="text" name="name" value="<?php echo $rows['name']; ?>" placeholder="name"/></td>





</tr>









<tr>





<td class="td_1">Email id :</td>









<td class="right"><input type="text" name="email" value="<?php echo $rows['email']; ?>" placeholder="email"/></td>





</tr>









<tr>





<td class="td_1">Date Of Birth :</td>









<td class="right"><input type="text" name="dob" value="<?php echo $rows['dob']; ?>" placeholder="dd/mm/yyyy"/></td>





</tr>









<tr>





<td class="td_1">Gender :</td>





<?php $sad=$rows['gender']; if($sad=="male"){$rs="selected";}else{$rs="";}if($sad=="female"){$acer="selected";}else{$acer="";} ?>





<td class="right">
<select name="gender">
<option value="male" <?php echo $rs;?> >Male</option>
<option value="female" <?php echo $acer;?> >Female</option>
</select>
</td>





</tr>









<tr>





<td class="td_1">Profession :</td>









<td class="right"><input type="text" name="profession" value="<?php echo $rows['profession']; ?>" placeholder="profession"/></td>





</tr>









<tr>





<td class="td_1">Location :</td>









<td class="right">
<textarea name="location" placeholder="location..."><?php echo $rows['location']; ?></textarea>
</td>





</tr>









<tr>





<td class="td_1">Website :</td>









<td class="right"><input type="text" name="website" value="<?php echo $rows['website']; ?>" placeholder="website"/></td>





</tr>









<tr>





<td class="td_1">About Me :</td>









<td class="right">
<textarea name="about_me" placeholder="About me..." rows="2" cols="25"><?php echo $rows['about_me'];?></textarea>
</td>





</tr>









<tr>





<th colspan="2"><input type="submit" name="submit" value="submit"/></th>





</tr>





</table>





</form>





</div>





</div>





</div>





</div>





</div>





Here we will display Image of users and allow them to change their profile pic. I am using a image plugin(ImageManipulator.php) written in php to easily handle any image. You can download it from here  and include it in your project.

updateac.php

<?php include('function.php'); user_session(); if(isset($_POST['submit'])){ $id=session_value("id"); $image=$_POST['image_file']; $name=$_POST['name']; $email=$_POST['email']; $dob=$_POST['dob']; $gender=$_POST['gender']; $profession=$_POST['profession']; $location=$_POST['location']; $website=$_POST['website']; $about_me=$_POST['about_me']; $image_row=user_info($id,"image_check"); if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { header("location: edit_profile.php?remarks=invalid_email"); }elseif($name=="") { header("location: edit_profile.php?remarks=invalid_email"); }else{ require_once('ImageManipulator.php'); $validExtensions = array('.jpg', '.jpeg', '.gif', '.png','.JPG', '.JPEG', '.GIF', '.PNG'); $fileExtension = strrchr($_FILES['image_file']['name'], "."); if (in_array($fileExtension, $validExtensions)) { $newNamePrefix = $id."_".time() . '_'; $manipulator = new ImageManipulator($_FILES['image_file']['tmp_name']); $width = $manipulator->getWidth();
$height = $manipulator->getHeight();
$newImage = $manipulator->resample(212,200,false);//Resizing Image to fit in profile
$manipulator->save('upload/pic_'.$newNamePrefix.$_FILES['image_file']['name']);// saving image to upload folder
$newImage = $manipulator->resample(120,120,false);//Shop resize 415px x 321px
$manipulator->save('upload/thumb_'.$newNamePrefix.$_FILES['image_file']['name']);// saving pic thumbnail to upload folder
$pic_url="".$newNamePrefix.$_FILES['image_file']['name']."";
echo '

success ...

';
} else {
echo '

You must upload an image...

';
}
if($pic_url==""){
$pic_url=user_info($id,"image");
}else{
$get_image=user_info($id,"image_check");
$image_url="upload/pic_".$get_image;
$image_thumb="upload/thumb_".$get_image;
unlink($image_url);
unlink($image_thumb);
}

mysql_query("Update member set image_url='$pic_url',name='$name',email='$email', dob='$dob',gender='$gender',profession='$profession', location='$location',
website='$website', about_me='$about_me' where mem_id='$id'");

header("location: welcome.php?remarks=updated");
}
}else{
header("location: welcome.php?remarks=access_denied");
}

?>

As our main aim is to allow user to search others profile in this tutorial. We will add script to process our search query. Copy and paste following code in search.php:

search.php

<?php include('function.php'); user_session(); user_check(""); ?>





<div class="tp-contentwrap1">





<div class="left wt_25 side_1">





<div class="image_block">
<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
</div>









<div class="menu_list">





<li><a href="edit_profile.php">Edit Profile</a></li>









<li><a href="deleteac.php">Delete Account</a></li>





</div>





</div>









<div class="right wt_75">
<?php $id=session_value("id"); $user=$_POST['user']; $sql="SELECT * FROM member where name LIKE '%$user%' and username LIKE '%$user%'"; $result=mysql_query($sql); ?>





<div class="tp-contentwrap2">





<div class="strip-profile">You searched for terms related to "<?php echo $user;?>"</div>









<table>
<?php while($rows=mysql_fetch_array($result)){ $id=$rows['mem_id']; ?>





<tr>





<td class="td_1">
<?php $image=user_info($id,"image_thumb");?>
<a href="profile.php?id=<?php echo $id;?>" target="_blank"><img src="<?php echo $image;?>" width="50px"/></a>
</td>









<td class="left"><a href="profile.php?id=<?php echo $id;?>" target="_blank"><?php echo $rows['username']; ?></a></td>





</tr>





<?php } ?>
</table>





</div>





</div>





</div>





When any user will click on user image or username displayed on search.php he will be redirected to his profile. To make it happen we will create a new page called profile.php :

profile.php

<?php include('function.php'); user_session(); user_check(""); ?>





<div class="tp-content">





<div class="left tp-content">





<div class="wt_99 center">





<div class="navigator left">





<ul>





<li><a href="index.php">HOME</a></li>





<span> &raquo; </span>





<li>Display user image on search in php.</li>





</ul>





</div>





</div>









<div class="wt_99 center left">





<div class="right search_form">





<form action="search.php" method="post">
<input type="text" name="user" placeholder="Search.."/>
<input type="submit" name="submit" value="Search"/>
</form>





</div>





</div>









<div class="tp-contentbox">





<div class="tp-infostrip">





<nav>





<ul>





<li class="left"><a href="welcome.php">Profile</a></li>









<li class="left"><a href="logout.php">Logout</a></li>









<li class="right"><a href="welcome.php">Hello <?php echo session_value("username"); ?></a></li>









<ul>
</nav>





</div>









<div class="tp-contentwrap1">





<div class="left wt_25 side_1">





<div class="image_block">
<img src="<?php echo user_info(session_value("id"),"image_url");?>"/>
</div>









<div class="menu_list">





<li><a href="edit_profile.php">Edit Profile</a></li>









<li><a href="deleteac.php">Delete Account</a></li>





</div>





</div>









<div class="right wt_75">
<?php $id=$_GET['id']; $sql="SELECT * FROM member where mem_id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?>





<div class="tp-contentwrap2">





<div class="strip-profile"><?php echo $rows['username']."'s";?> Profile</div>









<table>
<?php $id=$rows['mem_id']; ?>





<tr>





<th class="td_1" colspan="2">
<center><?php $image=user_info($id,"image_thumb");?>
<a href="" target="_blank"><img src="<?php echo $image;?>" width=""/></a></center>
</th>





</tr>









<tr>





<td class="td_1">Username :</td>









<td class="left"><?php echo $rows['username']; ?></td>





</tr>









<tr>





<td class="td_1">Name :</td>









<td class="left"><?php echo $rows['name']; ?></td>





</tr>









<tr>





<td class="td_1">Email id :</td>









<td class="left"><?php echo $rows['email']; ?></td>





</tr>









<tr>





<td class="td_1">Date Of Birth :</td>









<td class="left"><?php echo $rows['dob']; ?></td>





</tr>









<tr>





<td class="td_1">Gender :</td>









<td class="left"><?php echo $rows['gender']; ?></td>





</tr>









<tr>





<td class="td_1">Profession :</td>









<td class="left"><?php echo $rows['profession']; ?></td>





</tr>









<tr>





<td class="td_1">Location :</td>









<td class="left"><?php echo $rows['location']; ?></td>





</tr>









<tr>





<td class="td_1">Website :</td>









<td class="left"><?php echo $rows['website']; ?></td>





</tr>





</table>









<div class="wt_99 left">
<span class="td_1">About me</span>
<span class="left about"><?php echo $rows['about_me']; ?></span>
</div>





</div>





</div>





</div>





</div>





</div>





</div>





Thank you for reading my tutorial and i hope that you have liked it. Give you important feedback below in comment box.

21 Replies to “Display user’s profile with image on search in php”

  1. You must be a very kind person. Thanks for this great job. Once am done with the project am working on I will always link your site. Thanks once more.

  2. Thanks for the comprehensive tutorial. But please if I want to have every active user to show to others that the next person is also online in a list how can I do that.

  3. Hey thanks this is are really good 😀
    However, i would like to ask, how about adding admin account and pages which is only Admin that can access the pages ?

    1. simply add one more column account_type in users table and set 0 for normal user and 1 for administrator. This way you can differentiate between different type of users.

    2. Thanks for answering again 😀
      I just dont get the code in logincheck.php and manipulator in updateac.php.
      Can you describe it more clearly ?

      Also my request, if you ever share a codes again, please include a comment in the code, so we know how the code’s algorithm and how it works.

      anyway, Thanks.

  4. Dear Rahul Ranjan, Thanks for your tutorial post. But i am facing problem to put data post. i wrote some data in sign up form but no data stored in mysql. please may some problem in logincheck.php or index.php or function.php. please help me. thanks

    1. create one database with any name and set it in function.php as given below:
      $mysql_database = “your database name”;
      and import table.sql in that database.

  5. Thanks a lot for this post on user profile search with image. It serves my requirement. Another great post in the user profile series. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.