Hello DaniWeb Users;
I'm not a good coder here please hold on me,
My machine is linux ubuntu 14.04 and I install apache2 also the php5.
I was trying to create a simple log in page here (no registration thing)
So in mysql I just create 3 fields (user_id, username, password)
This is my connection script called dbconnect.php
<?php
$servername = "localhost";
$username = "root";
$password = "pagasa";
// Create connection
$conn = new mysql($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
and this is my log in script called login.php
<?php
ob_start();
session_start();
require_once 'dbconnect.php';
if ( isset($_SESSION['user'])!="" ) {
header("Location: home.php");
exit;
}
if ( isset($_POST['btn-login']) ) {
$uname = $_POST['username'];
$upass = $_POST['password'];
$uname = strip_tags(trim($uname)));
$upass = strip_tags(trim($upass)));
$password = hash('pagasa', $upass);
$res=mysql_query("SELECT password FROM user WHERE username='$uname'");
$row=mysql_fetch_array($res);
$count = mysql_num_rows($res);
if ( $count == 1 && $row['password']==$password ) {
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
} else {
$errMSG = "Not valid, Please Try again..!";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html"; charset=utf-8 />
<title>Login Page</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="container">
<div id="login-form">
<form method="post" autocomplete="off">
<div class="col-md-12">
<div class="form-group">
<h2 class="">Sign In.</h2>
</div>
<div class="form-group">
<hr />
</div>
<?php
if ( isset ( $errMSG ) ) {
?>
<div class="form-group">
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <?php echo $errMSG; ?>
</div>
</div>
<?php
}
?>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input type="text" name="username" class="form-control" placeholder="Your Username" required />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input type="password" name="password" class="form-control" placeholder="Your Password" required />
</div>
</div>
<div class="form-group">
<hr />
</div>
<div class="form-group">
<button type="submit" class="btn btn-block btn-primary" name="btn-login">Sign In</button>
</div>
<!--This will be add if Registration will be implement
<div class="form-group">
<a href="register.php">Sign Up..</a>
</div>
-->
</div>
</form>
</div>
</div>
</body>
</html>
So I just created a simple homepage called home.php
<?php
ob_start();
session_start();
require_once 'dbconnect.php';
if ( !isset ( $_SESSION['user'] ) ) {
header("Location: index.php");
exit;
}
$res=mysql_query("SELECT * FROM user WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html"; charset=utf-8" />
<title>Welcome to Test Homepage - Mr/Mrs. <?php echo $userRow['username']; ?> </title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
Test Only
</body>
</html>
Afterwards I run my browser (firefox) localhost/Test/
And click the login.php and gives me a blank page.
I can't see my error here is it in my script or do I miss anything here?
I do run .html file extensions and yes I can run it successfully.
But not on .php file extensions.
Please help to find what could be the problem and please review my code/script coz I'm not good in programming.
Thank you and God Bless.