20 - PHP Project - My PHP Site

In My PHP site project , HTML, CSS (style.css) and PHP code is used to create small site, where a user can register, search registered users by clicking on search link and login in to site. The site has three main parts: header, footer and mainmenu

After Signing into the site a user can upload files also and the files will be saved in Userimages folder.

When user open file index.php in Browser, the following page will be displayed:

Fig - Index.php

Here 3 links are available: Register, Search and Login when user clicks on Register link (Register.php), a form will be displayed as:

Fig - Register.php

 

When user enters the details in form and click on Register button, the contents are transferred in database and saved (Register_action).

Register_action

A user can also Login into site by clicking on Login link (login.php). At the login time the following interface will be displayed:

Fig - Login.php

Login credentials will be verified using login_action.php file. If email and password combination is correct, authentication will be successful and following page will be displayed:

Fig - Login_action.php

In this window Exit open will be displayed, by clicking on this option user will exit (exit.php) from this site.

Search link is used to search a user by name. (search.php)

For search method two files are used search_result and search_full_result

 

Fig - Search results

If search is successful user’s details will be displayed as:

To connect with database db.php file is used where connection code is present.

Index.php

<?php
    session_start();
    include "src/header.php";
    include "src/mainmenu.php";
?>
    <h2>Welcome to My PHP Site!!!</h2>

    <p>
       Your can start to <a href="register.php">register</a> as a new user.
       Then, you can <a href="search.php">search</a> others users.
       And you can <a href="login.php">login</a> to access your private area.
    </p>
<?php    
    include "src/footer.php";
?>

Register.php

<?php
    session_start();
    include "src/header.php";
    include "src/mainmenu.php";
?>
    <form method="post" action="register_action.php">
    <fieldset>
    <legend>Register</legend>
    <p>
        <label for="name">Full name:</label> <input type="text" name="name" id="name" /> 
    <br>
        <label for="email">Email:</label> <input type="email" name="email" id="email" /> 
    <br>
        <label for="password1">Password:</label> <input type="password" name="password1" id="password1" />
    <br>
        <label for="password2">Confirm password:</label> <input type="password" name="password2" id="password2" />
    <br>
        <label for="date_of_birth">Date of birth (yyyy-mm-dd):</label> <input type="date" name="date_of_birth" id="date_of_birth" />
    <br>
        <label for="place_of_birth">Place of birth:</label> <input type="text" name="place_of_birth" id="place_of_birth" />
    <br>
        <label for="info">Information:</label> <textarea name="info" id="info" rows="5" cols="50"></textarea>
    <br>
        <label for="nationality">Nationality:</label> <input type="text" name="nationality" id="nationality" />
    </p>

    <p class="center"><input value="Register" type="submit" /></p>
    </fieldset>
    </form>
<?php    
    include "src/footer.php";
?>

Register_action.php

?php 
    include 'db.php';
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password1 = $_POST['password1'];
    $password2 = $_POST['password2'];
    $date_of_birth = $_POST['date_of_birth'];
    $place_of_birth=$_POST['place_of_birth'];
    $info = $_POST['info'];
    $nationality=$_POST['nationality'];

     if ($password1 != $password2) {
        include "src/header.php";
        include "src/mainmenu.php";
        echo '<p>Error: password does not match. Try again</a>';
        echo '<p><a href="register.php">Try again</p>';
        include "src/footer.php";
        exit;
    }

    $sql = "INSERT INTO users (name, email,password1,password2,date_of_birth,place_of_birth,info,nationality) VALUES 
('$name','$email','$password1','$password2','$date_of_birth','$place_of_birth','$info','$nationality')";
    $result = mysql_query($sql, $link);

    if ($result == false) {
        include "src/header.php";
        include "src/mainmenu.php";
        echo '<p>Error: cannot execute query</p>';
        echo '<p><a href="register.php">Try again</a></p>';
        include "src/footer.php";
        exit;
    }
    else {
        header('Location: private.php');
    }
     mysql_close($link);
?>

Login.php

<?php
    session_start();
    include "src/header.php";
    include "src/mainmenu.php";
?>
    <form method="post" action="login_action.php">
    <fieldset>
    <legend>Login</legend>
    <p><label for="email">Email:</label> <input type="text" name="email" id="email" /></p>
    <p><label for="password">Password1:</label> <input type="password" name="password1" id="password1" /></p>
    <p class="center"><input value="Login" type="submit" class="center" /></p>
    </fieldset>
    </form>

<?php 
    include "src/footer.php";
?>

Login_action.php

<?php
    session_start();
    include 'db.php';
    error_reporting(0);
    $email = $_POST['email'];
    $password1 = $_POST['password1'];
    $sql = "select email, password1 from users where email = '$email' and password1 ='$password1'";
    $result = mysql_query($sql);
    if ($result == false) 
    {
        echo '<a href="login.php">Error: cannot execute query</a>';
        exit;
    }
    $num_rows = mysql_num_rows($result);

     if ($num_rows >= 1) {
        $_SESSION['login'] = "OK";
        $_SESSION['email'] = $email;
        header('Location: private.php');
        mysql_close($link);
        die();
    }

    mysql_close($link);

    header('Location: login.php');
?>

Search.php

<?php
    session_start();

     include "src/header.php";
    include "src/mainmenu.php";
?>
    <form method="post" action="search_result.php">
    <fieldset>
    <legend>Search</legend>
    <p><label for="name">Name:</label> <input type="text" name="name" id="name" /></p>
    <p class="center"><input type="submit" value="Search" /></p>
    </fieldset>
    </form>
<?php    
    include "src/footer.php";
?>

Search_full_result.php

<?php
    session_start();

    include "src/header.php";
    include "src/mainmenu.php";

    include 'db.php';

    echo "<fieldset><legend>Users</legend>";

    if(!isset($_POST['name']) || empty($_POST['name'])) {
        echo "<p>Empty search is not allowed</p>";
    }
    else { 
          $name = $_POST['name'];
            $sql = "select name, date_of_birth from users where name like '%$name%';";
        $result = mysql_query($sql, $link);
        if ($result == false) {
            echo '<p>Error: cannot execute query</p>';
        }
        else {
            $num_rows = mysql_num_rows($result);
            if($num_rows >= 1) {
                while($row = mysql_fetch_array($result))
                {
                   echo "<p>";
                    echo "<b>Name:</b> " . "<a href=\"search_full_result.php?name={$row["name"]}\">" .$row["name"] . "</a><br />";
                    echo "<b>Date of birth:</b> " . $row["date_of_birth"];
                    echo "</p>";
                }
            }
            else 
            {
                echo '<p>No user found</p>';
            }
        }
    } 
    mysql_close($link);
    echo "</fieldset>";
    include "src/footer.php";
?>

  Validate.php

<?php
    if(!(isset($_SESSION['login']) && $_SESSION['login'] == "OK")) {
        header('Location: login.php');
        exit;
    }
?>

Db.php

<?php
    $host="localhost";
    $username="root";
    $password="";
    $link = mysql_connect("$host","$username","$password");
    
    if ($link == false) {
        echo "Error: can't connect to database server";
        exit;
    }

    if (mysql_select_db("minifacebook2", $link) == false) {
        echo "Error: can't connect to database";
        exit;
    }

?>

Exit.php

<?php
    session_start();
    if(session_destroy())
    header("Location: login.php");
?>

Private.php

<?php
    session_start();
    error_reporting(0);    
    include "validate.php";
    include "src/header.php";
    include "src/mainmenu.php";
    include 'db.php';
$imgpath=$_FILES['file']['name'];
$email=$_POST['email'];
if($_POST['sub'])
{
    mysql_query("INSERT INTO image values('','$imgpath')");
    mkdir("userImages/$email");
    move_uploaded_file($_FILES["file"]["tmp_name"], "userImages/$un/" . $_FILES["file"]["name"]);
    $_SESSION['sname']=$_POST['email'];    
    echo "Your image was inserted in the records";
}
?>
    <h2>Welcome!</h2>
<form method="post" enctype="multipart/form-data">
<tr>
    <td height="55">Upload Your Pics</td>
    <td>
    <input type="file" name="file"/>
    </td>
  </tr>
  <tr>
    <td height="36">Insert Your image</td>
    <td>

    </td>
  </tr>
  <tr>
<tr>
    <td align="center" colspan="2">
    <input type="submit" name="sub" value="Submit"/>
    <input type="reset"  value="Reset"/>
    </td>
  </tr>
</form>
   <p><a href="exit.php">Exit</a></p>
<?php    
    include "src/footer.php";
?>

Footer.php

<footer>
    <p>Copyright &copy; 2014 PHP Development</p>
    <p><a href="mailto:ravita123@gmail.com">Contact</a></p>
</footer>

</div>
</body>
</html>

Header.php

<!DOCTYPE html>
<html>
<head>
<meta charset="iso-8859-1" />
<title>My PHP Site</title>
<link rel="stylesheet" href="css/styles.css" type="text/css" media="all" />
</head>
<body>
<div id="container">
<header>
<h1><a href="index.php">My PHP Site</a></h1>
</header>

Mainmenu.php

<nav>
<ul id="main_menu">
    <li><a href="register.php" title="Register new user">Register</a></li>
    <li><a href="search.php" title="Users list">Search</a></li>
    <li><a href="login.php" title="Login private area">Login</a></li>
    <?php
      if(isset($_SESSION['login']) && $_SESSION['login'] == "OK")
     {
        echo '<li><a href="private.php" title="Private area">Private</a></li>';
        echo '<li style="padding-left: 40px">' . $_SESSION['email'] . '</li>';
      }
    ?>
</ul>
</nav>

Style.css

body
{
  font-family: Arial, sans-serif;
  overflow: scroll;
}

header {
  display: block;
  border-radius: 10px;
  background-color: #00f;
  width: 100%;
  height: 100px;
  color: #fff;
}

header h1 {
  text-align: center;
  font-size: 5em;
}

header h1 a {
  color: #fff;
  text-decoration: none;
}

fieldset {
  border-radius: 10px;
  border: 1px solid #00f;
}

legend {
  color: #00f;
}

label
{
  float: left;
  width: 40%;
  margin-right: 0.5em;
  padding-top: 0.2em;
  text-align: right;
}

textarea
{
  float: left;
  width: 40%;
  margin-right: 0.5em;
  padding-top: 0.2em;
}
input
{
  width: 20%;
  padding-top: 0.2em;
  text-align: middle;
}

footer
{
  display: block;
  border: solid 1px #00f;
  border-radius: 10px;
  padding: 10px;
  margin-top: 10px;
}

.center {
  text-align: center;
}

#container
{
  width: 700px;
  margin-left: auto;
  margin-right: auto;
}

#main_menu
{
  list-style-type: none;
  background-color: #ccf;
  padding: 10px;
  border-radius: 10px;
}

#main_menu > li
{
  display: inline-block;
}

 

Like us on Facebook