Você está na página 1de 6

CONN.

PHP
<?php
$db_host= "localhost";
$db_username= "root";
$db_pass= "";
$db_name= "test";

mysql_connect("$db_host","$db_username","$db_pass") or die("could not connect


to mysql");
mysql_select_db("$db_name") or die ("no database");
?>

INDEX.PHP
<?php
include ("conn.php");
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
echo"<table border='0' width='800' align='center'>";
echo"<tr bgcolor='#3399FF'>";
echo"<th width='90' >ID </th>";
echo"<th width='100'>User Name</th>";
echo"<th width='90'>Password</th>";
echo"</tr><br>";
while ($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>". $row['ID'] . "</td>";
echo "<td><center>". $row['Uname'] . "</td>";
echo "<td><center>". $row['Password'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<form action="register.php" method="post" name="form2">
<br>
<br>
<table align="center" width="320" border="0" bgcolor="#CCFFFF">
<tr>
<td colspan="2"><h3>Add User: </h3></td>

</tr>
<tr>
<td>User Name: </td>
<td><input name ="UName" type="text"/></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="Password" type="password"/></td>
</tr>
<tr>
<td>&nbsp;<td>
<td><input type="submit" name="Submit"
value="Register"/> </td>
</tr>
</table>
</form>

<form method="post" action="login.php" name="form3">


<br>
<br>
<table align="center" width="320" border="0" bgcolor="#99FF99">
<tr>
<td colspan="2"><h3>Login: </h3></td>
</tr>
<tr>
<td>Username: </td>
<td><input name ="UserName" type="text"/></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="Password" type="password"/></td>
</tr>
<tr>
<td>&nbsp;<td>
<td><input type="submit" name="Submit"
value="Login"/> </td>
</tr>
</table>
</form>

LOGIN.PHP

<?php
session_start();
if(count($_POST)>0) {
$conn = mysql_connect("localhost","root","");
mysql_select_db("test",$conn);
$result = mysql_query("SELECT * FROM users WHERE Uname='" .
$_POST["UserName"] . "' and Password = '". $_POST["Password"]."'");
$row = mysql_fetch_array($result);
if(is_array($row)) {
$_SESSION["ID"] = $row['ID'];
$_SESSION["UserName"] = $row['Uname'];
} else {
echo '<script language="javascript">';
echo 'alert("Incorrect Username or Password!!!")';
echo '</script>';
header( "refresh:0.5;url=http://localhost/finals/index.php" );
}
}
if(isset($_SESSION["ID"])) {
header( "refresh:0.5;url=http://localhost/finals/home.php" );
echo '<script language="javascript">';
echo 'alert("Login Successful!!!")';
echo '</script>';
}
?>
HOME.PHP
<?php
session_start();
?>
<html>
<head>
<title>User Login</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
<tr class="tableheader">
<td align="center">User Dashboard</td>
</tr>
<tr class="tablerow">
<td>
<?php
if($_SESSION["UserName"]) {
?>
Welcome <?php echo $_SESSION["UserName"]; ?>. Click here to <a
href="logout.php" tite="Logout">Logout.

<?php
}
?>
</td>
</tr>
</body>
</html>
REGISTER.PHP
<html>
<body>
<?php include ("conn.php");
$Uname=$_POST['UName'];
$Password= $_POST['Password'];
$sql="SELECT * FROM users WHERE Uname = '".$Uname."'";
$result=mysql_query($sql) or die ('error');
$row= mysql_fetch_assoc($result);
if(mysql_num_rows($result)) {
//Redirecting Timer
echo '<script type="text/javascript">
(function () {
var timeLeft = 5,
cinterval;
var timeDec = function (){
timeLeft--;
document.getElementById("countdown").innerHTML = timeLeft;
if(timeLeft === 0){
clearInterval(cinterval);
}
};
cinterval = setInterval(timeDec, 1000);
})();
</script>
<br><br><br><br><br><br><br><center>Redirecting in <span
id="countdown">5</span>.</center>';
echo'&nbsp <center>If the browser not redirects <a
href="http://localhost/finals/index.php">Click Here to Return </a></center>';
echo '<script language="javascript">';
echo 'alert("Already Exists!!")';
echo '</script>';
header( "refresh:5;url=http://localhost/finals/index.php" );
}

else{
mysql_query("INSERT INTO users(ID,Uname,Password) VALUES
('','$_POST[UName]','$_POST[Password]')");
echo '<script type="text/javascript">
(function () {
var timeLeft = 5,
cinterval;
var timeDec = function (){
timeLeft--;
document.getElementById("countdown").innerHTML = timeLeft;
if(timeLeft === 0){
clearInterval(cinterval);
}
};
cinterval = setInterval(timeDec, 1000);
})();
</script>
<br><br><br><br><br><br><br><center>Redirecting in <span
id="countdown">5</span>.</center>';
echo'&nbsp <center>If the browser not redirects <a
href="http://localhost/finals/index.php">Click Here to Return </a></center>';
echo '<script language="javascript">';
echo 'alert("User Added Successfully!!")';
echo '</script>';
header( "refresh:5;url=http://localhost/finals/index.php" );
}
?>
</body>
</html>

LOGOUT.PHP
<?php
session_start();
unset($_SESSION["ID"]);
unset($_SESSION["Uname"]);
header("Location:index.php");
?>

STYLES.CSS
.tableheader {
background-color: #95BEE6;
color:white;
font-weight:bold;
}
.tablerow {
background-color: #A7D6F1;
color:white;
}
.message {
color: #FF0000;
font-weight: bold;
text-align: center;
width: 100%;
}

Você também pode gostar