Create a simple user profile system

This post guides you to create you own simple user profile system. Using this system, your users can register and sign in. The complete system is based of PHP and MYSQL. This system contains mainly 3 files, firstly the config.php, secondly the index.php and lastly the profile.php.  After uploading the files to your web server, first edit the details of the config.php file, namely the database user,name and password. The run in on the web browser, just like http://www.xyz.com/config.php/ now you are just good to go. Your users can access the system by just going to your url. i.e. http://www.xyz.com/.

Demo



The Homepage of the System, showing both sign in and sign up buttons.


The Profile Page of the System, Showing the username and the last login details.



PHPMyAdmin panel showing the data entered in the database.



Lets Create the config.php file.

<?php
$dbhost = 'localhost';// Database Host.
$dbuser = 'sid';// Database User.
$dbpass = 'sp123';// User's Password.
$dbname = 'sample'; // Database Name
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn)
{
	die('Could not connect: ' . mysql_error());
}
$sql='CREATE TABLE USER ('.
	 'username VARCHAR(20)  NOT NULL,'.
	 'password VARCHAR(20)  NOT NULL,'.
	 'primary key(username))';
mysql_select_db($dbname);
$retval=mysql_query($sql,$conn);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table student created successfully<br>";
?>


Lets Create the index.php file.
<?php
$dbhost = 'localhost';// Database Host.
$dbuser = 'sid';// Database User.
$dbpass = 'sp123';// User's Password.
$dbname = 'sample';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
	die('Could not connect: ' . mysql_error());
}
if(isset($_POST['in']))
{
	$sql = 'SELECT username, password from USER where username=\''.$_POST['name'].'\'';
	mysql_select_db($dbname);
	$retval = mysql_query( $sql, $conn );
	if(! $retval )
	{
		echo 'Invalid Username Entered, Please Check Again';
	}
	$row = mysql_fetch_row($retval);
	//echo $row[0].' '.$row[1];
	if($_POST['pass']==$row[1])
	{
		echo 'Logging you please wait !';
		session_start();
		$_SESSION['user'] = $_POST['name'];
		$_SESSION['time'] = time();
		?>
		<meta http-equiv="refresh" content="3;URL=profile.php" />
		<?php
	}
	else
	{
		echo 'Invalid Username Entered, Please Check Again';
	}
}
else if (isset($_POST['up']))
{
	$sql = "INSERT INTO user ".
       "VALUES('".$_POST['name']."','".$_POST['pass']."')";
	mysql_select_db($dbname);
	$retval = mysql_query( $sql, $conn );
	if(! $retval )
	{
		die('Could not get data: ' . mysql_error());
	}
	echo 'Logging you please wait !';
		session_start();
		$_SESSION['user'] = $_POST['name'];
		$_SESSION['time'] = time();
		?>
		<meta http-equiv="refresh" content="3;URL=profile.php" />
		<?php
}
?>
<html>
<head>
<title> User Profile System</title>
<style>
#main_box
{
  position:relative;
  top:200px;
  background-color: cadetblue;
  width: 200px;
  height: 100px;
  PADDING: 10 10 10 10;
  BORDER-RADIUS: 10px;
  box-sizing: content-box;
  box-shadow: 2px 2px 2px chocolate;
}
.inp_st
{
  position: relative;
  border-radius: 5px;
  padding: 2 2 2;
  box-shadow: 2px 2px 2px chocolate;
}
.inp_bt
{
  position: relative;
  top: 25px;
  border-radius: 5px;
  box-shadow: 2px 2px 2px aqua;
}
</style>
</head>
<body>
<center>
<b style="
    position: relative;
    top: 180px;
    font-size: -webkit-xxx-large;
    font-family: cursive;
    color: cadetblue;
">User Profile System</b>
<form action="<?php $_PHP_SELF ?>" method ="post" id="main_box">
<center><input style="top:14px;" type="text" name="name" autocomplete="off" placeholder="Username" required class="inp_st"/><br/>
<input type="password" style="top:18px;" autocomplete="off" name="pass" placeholder="Password" required class="inp_st"/><br/>
<input type="submit" value="Sign In" class="inp_bt" name="in"/>
<input type="submit" value="Sign Up" class="inp_bt" name="up"/>
</center>
</form></center>
</body>
</html>

Lets Create the profile.php file.
<html>
<head>
<title>Profile Page</title>
</head>
<?php
if(isset($_POST['log']))
{
	echo 'Logging you out, please wait !';
	
	?><meta http-equiv="refresh" content="3;URL=index.php" />
	<?php
}
?>
<body>
<center>
<b style="
    position: relative;
    top: 140px;
    font-size: -webkit-xxx-large;
    font-family: cursive;
    color: cadetblue;
">User Profile System</b><br/>

<?php
session_start();
echo '<b style="
    position: relative;
    top: 180px;
    font-size: x-large;
    font-family: cursive;
    color: cadetblue;
">Hello '.$_SESSION['user'].', Welcome! ;)</b></center>'; // Username
?>

<b style="font-family: cursive; position:relative; left:20%;top: 210;">
<form action="<?php $_PHP_SELF ?>" method ="post">
<input style="
    position: relative;
    top: 35px;
	  z-index: 1;
  background-color: aliceblue;
  border-bottom-color: aliceblue;
"type="submit" value="Logout" name="log"/>
</form>
</b>
<div style="position:relative; width:100%;top: 210;background-color: aliceblue;">
<b style="font-family: cursive; color: cadetblue; position:relative; left:70%;"> Last Login: <?php echo date('H:i', $_SESSION['time']); ?> GMT</b></div>


</body>
</html>

Alternatively you can download the whole 3 files in a single zip by clicking here.

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution