We need to create 6 files to complete this task which are:
1.index.html
2.config.php
3.check.php
4.session.php
5.profile.php
6.logout.php
The codes for each file given below:
index.html
This will be tour main page.
<html>
<head>
<title>online chat</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="main">
<div id="container">
<div class="login">
<form name="login" method="post" action="check.php">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input type="submit" value="Login" name="submit">
</form>
</div>
</div>
</div>
</body>
</html>
Now we create an connection with the database.
config.php
<?php
mysql_connect("localhost","root","");
$a=mysql_select_db("chat");
if(!$a)
{
echo"error";
}
?>
in above file chat is the name of database i used.
Now create the file where the username and password is checked and redirected to profile page.
check.php
<?php
include('config.php');
session_start();
$error='';
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
echo $username. $password;
$query = mysql_query("select * from user_login where username='$username' AND pass='$password' ");
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username;
header("location: profile.php");
} else {
$error = "Username or Password is invalid";
}
}
}
?>
The above file check for username and password if the are correct it redirect to profile.php other wiise shows an error.
Before creating an profile .php we create session.php which starts an session.
session.php
<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysql_query("select username from user_login where username='$user_check'");
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
header('Location: index.php');
}
?>
here if session is set and if it is not than it redirects to index.html or index.php where you want to.
then we create profile.php
profile.php
<?php
include('session.php');
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script>
</script>
</head>
<body>
<div id="profile">
<div class="head">
<a href="logout.php">Log Out</a>
</div>
<div class="friend">
<a href="">Amit</a>
</div>
<div class="chat">
<div class="chat_head">
</div>
<div class="chat_content">
</div>
<div class="chat_text">
<textarea placeholder="Type Here" name="chat_text"></textarea>
</div>
</div>
</div>
</body>
</html>
And in last we create the logout.php
logout.php
<?php
include('config.php');
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
The css can be done on your requirements.
1.index.html
2.config.php
3.check.php
4.session.php
5.profile.php
6.logout.php
The codes for each file given below:
index.html
This will be tour main page.
<html>
<head>
<title>online chat</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="main">
<div id="container">
<div class="login">
<form name="login" method="post" action="check.php">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input type="submit" value="Login" name="submit">
</form>
</div>
</div>
</div>
</body>
</html>
Now we create an connection with the database.
config.php
<?php
mysql_connect("localhost","root","");
$a=mysql_select_db("chat");
if(!$a)
{
echo"error";
}
?>
in above file chat is the name of database i used.
Now create the file where the username and password is checked and redirected to profile page.
check.php
<?php
include('config.php');
session_start();
$error='';
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
echo $username. $password;
$query = mysql_query("select * from user_login where username='$username' AND pass='$password' ");
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username;
header("location: profile.php");
} else {
$error = "Username or Password is invalid";
}
}
}
?>
The above file check for username and password if the are correct it redirect to profile.php other wiise shows an error.
Before creating an profile .php we create session.php which starts an session.
session.php
<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysql_query("select username from user_login where username='$user_check'");
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
header('Location: index.php');
}
?>
here if session is set and if it is not than it redirects to index.html or index.php where you want to.
then we create profile.php
profile.php
<?php
include('session.php');
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script>
</script>
</head>
<body>
<div id="profile">
<div class="head">
<a href="logout.php">Log Out</a>
</div>
<div class="friend">
<a href="">Amit</a>
</div>
<div class="chat">
<div class="chat_head">
</div>
<div class="chat_content">
</div>
<div class="chat_text">
<textarea placeholder="Type Here" name="chat_text"></textarea>
</div>
</div>
</div>
</body>
</html>
And in last we create the logout.php
logout.php
<?php
include('config.php');
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
The css can be done on your requirements.
No comments:
Post a Comment