PHP Echo Returns Blank Page - php

I have "checks" in here and if something happens, it will echo a phrase like The username you supplied is already in use or whatever. When it DOES display it though, it'll only display it on a blank page with that text. What is wrong here?
PHP
<?php
include('./dbconnect/global.php');
if ($_POST['register']) {
$username = mysql_real_escape_string(strip_tags($_POST['username']));
$password = mysql_real_escape_string(strip_tags($_POST['password']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
if (!$username||!$password||!$email)
echo "PLease Fill in The Required Fields";
else {
//check if username is taken
$check = mysql_query("SELECT * FROM users WHERE username='$username'");
if (mysql_num_rows($check) >= 1)
echo "The Username you Supplied Is Already in Use! <a href='./register'>Back</a>";
$check2 = mysql_query("SELECT * FROM users WHERE email='$email'");
if (mysql_num_rows($check) >= 1)
echo "The Email you Supplied Is Already in Use! <a href='./register'>Back</a>";
else {
$password2 = md5($password);
$register = mysql_query("value and stuff)") or die(mysql_error());
echo "Thanks for registering, $username! <a href='./index'>Home</a>";
}
}
}
else {
?>
HTML
<html>
<head>
<title>CoreCrafters</title>
<link rel="stylesheet" type="text/css" href="./css/main.css">
</head>
<body>
<?php include('./include/nbar.php') ?>
<div id="main">
<div id="register">
<div id="wrapper">
<form action="register" method="POST">
Username
<br />
<input type="username" name="username">
<br />
Password
<br />
<input type="password" name="password">
<br />
Email
<br />
<input type="email" name="email">
<br /><br />
<input type="submit" name="register" value="Register">
</form>
</div>
</div>
</div>
<?php include('./include/footer.php') ?>
</body>
</html>
Ending PHP (From ELSE at end)
<?php
}
?>
CSS
body {
margin: 0;
padding: 0;
background-color: #EEE;
}
#headerbar {
width: 100%;
background-color: #000;
}
a {
color: #069;
font-weight: bold;
text-decoration: none;
transition: all 0.2s linear;
-o-transition: all 0.2s linear;
-ms-transition: all 0.2s linear;
-moz-transition: all 0.2s linear;
-webkit-transition: all 0.2s linear;
}
a:hover {
color: #c00;
}
#nav {
padding: 0;
width: 100%;
float: left;
list-style: none;
margin: 0 0 3em 0;
background-color: #f2f2f2;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
#nav li {
float: left;
}
#nav li a {
color: #069;
display: block;
font-weight: bold;
padding: 8px 15px;
text-decoration: none;
transition: all 0.2s linear;
border-right: 1px solid #ccc;
-o-transition: all 0.2s linear;
-ms-transition: all 0.2s linear;
-moz-transition: all 0.2s linear;
-webkit-transition: all 0.2s linear;
}
#nav li a:hover {
color: #c00;
background-color: #fff;
}
#footer {
width: 94%;
padding: 10px;
margin-top: 10px;
margin-left: auto;
padding-left: 0px;
text-align: center;
margin-right: auto;
padding-right: 0px;
border: 1px solid #DDD;
background-color: #FFF;
}
#main {
width: 94%;
margin-top: 50px;
min-height: 500px;
margin-left: auto;
margin-right: auto;
border: 1px solid #DDD;
background-color: #FFF;
}
#register {
width: 15%;
margin-top: 20px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: #CCC;
border: 1px solid #AAA;
}
#logbar {
width: 150px;
float: right;
min-height: 36px;
padding-left: 5px;
padding-right: 50px;
background-color: #DDD;
}
As requested, I posted most of the page onto here.

Your webpage has no content except that echo, so it is echo-ing it on a blank page because you told it to.
Update: Code
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8"> <!-- UXSS UTF-7 (IE6) prevention -->
<title>CoreCrafters</title>
<link rel="stylesheet" type="text/css" href="./css/main.css">
</head>
<body>
<?php #include('./include/nbar.php'); ?>
<div id="main">
<div id="register">
<div id="wrapper">
<form method="POST">
Username<br /><input type="username" name="username"><br />
Password<br /><input type="password" name="password"><br />
Email<br /><input type="email" name="email"><br /><br />
<input type="submit" name="register" value="Register">
</form>
</div>
<?php
#include('./dbconnect/global.php');
if (isset($_POST['register'])) {
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
die('Please fill out the required fields.');
} else {
$query = mysql_query("SELECT * FROM users WHERE username = '" . mysql_real_escape_string($_POST['username']) . "'");
if (mysql_num_rows($query)) { die('The username you provided is already in use!'); }
$query = mysql_query("SELECT * FROM users WHERE email = '" . mysql_real_escape_string($_POST['email']) . "'");
if (mysql_num_rows($query)) { die('The email you provided is already in use!'); }
/** Insert values **/
echo 'Thanks for registering, ' . htmlentities($_POST['username'], ENT_QUOTES) . '!'; /** Normal XSS prevention **/
}
}
?>
</div>
</div>
<?php #include('./include/footer.php'); ?>
</body>
</html>

I believe you are missing semicolons for your include statements (inside the HTML block). Such as:
<?php include('./include/footer.php'); ?>
If that doesn't solve it, you should check your error log. A completely blank page is often what you get when the PHP parser was unable to parse your code.

Related

Links don't work or display correctly in PHP

I made a header design in html file and everything worked nice, but when I put it in .php file they became like idle so I can't click or hover them. PHP code:
<?php
session_start();
require_once "stuff.php";
if (isset($_SESSION['encnick'])){
header("Location: my.php");
return;
}
//On form submit
if (isset($_POST['register'])){
if (!isset($_POST['nick'])){
$_SESSION['error'] = "Nickname is required";
header("Location: register.php");
return;
}
if (!isset($_POST['password'])){
$_SESSION['error'] = "Password is required";
header("Location: register.php");
return;
}
if (!isset($_POST['cpassword'])){
$_SESSION['error'] = "Password is required";
header("Location: register.php");
return;
}
if ($_POST['password'] !== $_POST['cpassword']){
$_SESSION['error'] = "Passwords don't match";
header("Location: register.php");
return;
}
else{
$statement = $pdo->prepare("SELECT * FROM users WHERE nick = :nick");
$statement->execute(array(':nick' => $_POST['nick']));
$row = $statement->fetch(PDO::FETCH_ASSOC);
if ($row['nick'] === $_POST['nick']){
$_SESSION['error'] = "This nickname is already taken";
header("Location: register.php");
return;
}
$statement = $pdo->prepare("INSERT INTO users (nick, password, company, encodednick, user_group) VALUES (:nick, :password, :company, :encodednick, :user_group)");
$statement->execute(array(
':nick' => htmlentities($_POST['nick']),
':password' => password_hash($_POST['password'], PASSWORD_BCRYPT),
':company' => htmlentities($_POST['company']),
':encodednick' => md5($_POST['nick']),
':user_group' => "user"
));
$_SESSION['encnick'] = md5($_POST['nick']);
$encnick = $_SESSION['encnick'];
header("Location: my.php?id=$encnick");
return;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>ReactSynchro</title>
<link rel="stylesheet" href="css/register.css">
<link rel="stylesheet" href="css/fonts.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<?php printheader() ?>
<div id="formdiv">
<form method="post">
<label for="nick">Nickname</label>
<input type="text" name="nick" id="nick" placeholder="Enter unique nickname" minlength="6" maxlength="30" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="Enter password" minlength="8" maxlength="60" required>
<label for="cpassword">Confirm Password</label>
<input type="password" name="cpassword" id="cpassword" placeholder="Confirm password" minlength="8" maxlength="50" required>
<label for="company">Company</label>
<input type="text" name="company" id="company" placeholder="Enter your company" minlength="3" maxlength="50">
<input type="submit" value="Register" name="register" id="regbtn">
</form>
</div>
<?php
if (isset($_SESSION['error'])){
echo "<h2 id='errormessage'>".$_SESSION['error']."</h2>";
unset($_SESSION['error']);
}
?>
</body>
</html>
main.css code:
body{
background-color: #333;
margin: 0;
}
#header{
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: #444;
height: 110px;
}
#header img{
height: 100px;
position: relative;
left: 5px;
top: 5px;
bottom: 5px;
}
#header a{
color: white;
text-decoration: none;
display: block;
position: relative;
float: left;
text-align: center;
margin: 5px;
padding: 5px;
border: 0.5px solid white;
font-family: 'Ubuntu';
font-size: 20px;
-webkit-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
}
#hlinks1{
position: relative;
left: 120px;
top: -71px;
}
#hlinks2{
position: absolute;
right: 5px;
top: 50%;
transform: translateY(-50%);
}
#hlinks2 a{
background-color: rgba(255, 140, 0, 0.9);
}
#hlinks1 a:hover{
background-color: rgba(255, 255, 255, 0.1);
}
#hlinks2 a:hover{
background-color: rgba(255, 140, 0, 1);
}
#errormessage{
color: red;
text-align: center;
font-family: 'Ubuntu';
}
#media all and (max-width: 537px){
#hsupbtn{
visibility: hidden;
}
}
#media all and (max-width: 440px){
#hregbtn{
visibility: hidden;
}
}
register.css code:
#formdiv{
width: 50%;
height: 350px;
position: absolute;
left: 0;
right: 0;
margin: auto;
top: 50%;
transform: translateY(-50%);
border: 0.5px solid white;
max-width: 500px;
min-width: 250px;
}
#formdiv label{
text-align: center;
font-family: 'Ubuntu';
color: white;
width: 100%;
display: block;
font-size: 24px;
line-height: 50px;
}
#formdiv input{
width: 90%;
position: relative;
margin: auto;
display: block;
font-family: 'Ubuntu';
line-height: 20px;
}
#regbtn{
width: 50% !important;
top: 20px;
background-color: rgba(255, 140, 0, 0.9);
color: white;
-webkit-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
}
#regbtn:hover{
background-color: rgba(255, 140, 0, 1);
}
#formdiv input:empty{
border: 0.5px solid white;
}
#formdiv input:valid{
border: 0.5px solid green;
}
The menu printing function in stuff.php file:
function printheader(){
echo "<div id='header'>
<img src='images/logo.png'>
<div id='hlinks1'>
<a href='../index.php'>Home</a>
<a href='../about.html'>About</a>
<a href='../support.php' id='hsupbtn'>Support</a>
</div>
<div id='hlinks2'>
<a href='../register.php' id='hregbtn'>Register</a>
<a href='../login.php'>Login</a>
</div>
</div>
<div style='height: 110px; position: relative;'></div>";
}
So I mean that when I hover or click links nothing happens at all, they are idle like pictures, but if I disconnect main.css file they start working normally.
You have following in your printheader function
<div style='height: 110px; position: relative;'></div>
Remove the following:-
position: relative

CSS/HTML How To Display Form Data On A Specific Spot On The Web Page

I have a web page which displays data on the left side of the page and it goes down in a list.
I need help displaying a form in the middle/right side of the page using CSS. With the current CSS I have for the form, it is displayed at the bottom of the page which is wrong, I want it to be in the middle/right. The form is basically a search field where users can search for members on the website.
I'm bad with CSS, maybe someone can also suggest a cool style for the form? :D
The div class called searchList is where the form is that I want to align to the middle/right
My code:
<?php
include('init.inc.php');
$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); //filter, can remove
$query = mysql_query("SELECT * FROM users WHERE user_first LIKE '%$searchq%' OR user_last LIKE '%$searchq%' OR user_uid LIKE '%$searchq%'");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There was no search found!';
}else{
while($row = mysql_fetch_array($query)){
$uname = $row['user_uid'];
$fname = $row['user_first'];
$lname = $row['user_last'];
$email = $row['user_email'];
$id = $row['user_id'];
$output .= '<div> '.$uname.' '.$fname.' '.$lname.' '.$email.'</div>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns=""http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<section id="showcase1">
<section id="newsletter">
<div class="container">
<h1>ProjectNet Members:</h1>
</div>
</section>
<div class="container">
<div class="userList">
<?php
foreach (fetch_users() as $user){
?>
<p>
<button><a class="userList" href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a></button>
</p>
<?php
}
?>
</div>
</div>
<div class="searchList">
<form id="search" action="user_list.php" method="post">
<input type="text" name="search" placeholder="Search for members..." />
<input type="submit" value="Search" />
</form>
<?php print("$output");?>
</div>
</section>
</body>
</html>
CSS:
.searchList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 20px Arial, Helvetica,sans-serif;
text-align: center;
}
UPDATE:
I managed to get the form in the middle of the screen, I need help moving it slightly towards the right.
CSS:
.searchList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 19px Arial, Helvetica,sans-serif;
text-align: center;
left: 0;
line-height: 35px;
margin: auto;
margin-top: -100px;
position: absolute;
top: 50%;
width: 100%;
float: right;
}
#search input[type="submit"] {
cursor: pointer;
border: none;
background: #fafafa;
color: #333;
font-weight: bold;
margin: 0 0 5px;
padding: 3px;
font-size: 15px;
font: Arial, Helvetica,sans-serif;
}
#search input[type="text"] {
width: 18%;
border: 1px solid #CCC;
background: #FFF;
margin: 0 0 5px;
padding: 4px;
}
}
#search input[type="submit"]:hover {
background: #e8491d;
color: #fafafa;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
Use CSS position property or float property.
To display form on the right of the page use float : right;
form {
float : right;
}
Moving to the right, where the 4th value is the left padding.
padding: 0px 0px 0px 30px;
..alternatively you can use.
padding-left: 30px;
If you want to have better control of the layout structure I suggest you use "grid" which is embedded in css. Search for "css grid".

Why is it showing "Please Enter Username" instead of recording

I am writing a php file. It should read the username and password I enter and recored them to a txt file. It is only showing please enter username. It isn't showing my background colors either.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>NewHW3</title>
</head>
<body>
<style scoped>
body {font-family: Arial, Helvetica, sans-serif;}
form {border: 3px solid #f1f1f1;}
input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #333FFF;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}
button:hover {
opacity: 0.8;
}
.cancelbtn {
width: auto;
padding: 10px 18px;
background-color: #f44336;
}
.imgcontainer {
text-align: center;
margin: 24px 0 12px 0;
}
.container {
padding: 16px;
}
span.psw {
float: right;
padding-top: 16px;
}
/* Change styles for span and cancel button on extra small screens */
#media screen and (max-width: 300px) {
span.psw {
display: block;
float: none;
}
.cancelbtn {
width: 100%;
}
}
</style>
<h1>User Login</h1>
<form method="post" action="863l.php">
Username: <input type="text" name="uname" placeholder="Username">
Password: <input type="password" name="password" placeholder="Password">
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
That's the first code, it is supposed to display username and password. It seems to work but when I run it just says please enter username on the top.
<?php
$uname=$_POST['username'];
$password=$_POST['password'];
if ($uname==''){
echo "<script>alert('Please Enter Username');</script>";
exit();
}
if (!isset($_POST['username'])) ;{
}
if(!isset($_POST['password']));
$fp=fopen("863l.txt", "a");
$savestring="Username: ".$email."\n"."Password: ".$password."\n";
fwrite($fp, $savestring);
fclose($fp);
?>
The second code is supposed to record my input information into a txt file, it isn't doing that.
I didn't add the text file, bit it isnt recording to it either:
change this:
$uname=$_POST['username'];
to this:
$uname=$_POST['uname'];
because your input form like this
Username: <input type="text" name="uname" placeholder="Username">

Positioning the validation error message

I have this login form which looks like this:
When I enter a wrong information, it would look like this:
Now the problem is that when I get the error, it moves my login form down.
Is there anyway I could prevent the form from moving when I get the error message? Also maybe I could put the error message below the form but I don't know how to, I tried echoing the error below the form but it didn't work.
Here's my PHP:
<?php
/*********************************************************************
* This script has been released with the aim that it will be useful.
* Written by Vasplus Programming Blog
* Website: www.vasplus.info
* Email: info#vasplus.info
* All Copy Rights Reserved by Vasplus Programming Blog
***********************************************************************/
session_start();
ob_start();
//Include the database connection file
include "database_connection.php";
//Check to see if the submit button has been clicked to process data
if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
{
//Variables Assignment
$username = trim(strip_tags($_POST['username']));
$user_password = trim(strip_tags($_POST['passwd']));
$validate_user_information = mysql_query("select * from `signup_and_login_users_table` where `username` = '".mysql_real_escape_string($username)."' and `password` = '".mysql_real_escape_string($user_password)."'");
//Validate against empty fields
if($username == "" || $user_password == "")
{
$error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
}
elseif(mysql_num_rows($validate_user_information) == 1) //Check if the information of the user are valid or not
{
//The submitted info of the user are valid therefore, grant the user access to the system by creating a valid session for this user and redirect this user to the welcome page
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $username;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["fullname"]);
header("location: home.php");
}
else
{
//The submitted info the user are invalid therefore, display an error message on the screen to the user
$error = '<div class="info" style="color: red; text-align: center;">You have entered an invalid username or password</div>';
echo $error;
}
}
?>
Here's my HTML:
body {
background-image: url(img/hero.jpg);
background-size: cover;
font-family: Montserrat;
}
.logo {
width: 400px;
height: 200px;
background-image: url(img/corelogo.png);
background-size: cover;
margin: -20px auto;
}
.login-block {
width: 320px;
padding: 20px;
background: transparent;
border-radius: 5px;
border-top: 5px solid #011f4b;
margin: 0 auto;
font-family: Questrial;
}
.login-block h1 {
text-align: center;
color: #000;
font-size: 18px;
text-transform: capitalize;
letter-spacing: 2px;
margin-top: 0;
margin-bottom: 20px;
}
.login-block input {
width: 100%;
height: 42px;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #ccc;
margin-bottom: 20px;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
padding: 0 20px 0 50px;
outline: none;
}
.login-block input#username {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#username:focus {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input#password {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#password:focus {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input:active,
.login-block input:focus {
border: 1px solid #011f4b;
}
.login-block button {
width: 100%;
height: 40px;
background: #011f4b;
box-sizing: border-box;
border-radius: 5px;
border: 0px solid #1293e1;
color: #fff;
font-weight: 500;
text-transform: uppercase;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
outline: none;
cursor: pointer;
margin-bottom: 10px;
}
.login-block button:hover {
background: #1293e1;
}
<link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<center>
<div style="font-family:Questrial, sans-serif; font-size:24px;"></div><br clear="all" /><br clear="all" />
<!-- Code Begins -->
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<div class="logo"></div>
<div class="login-block">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Employee Login</h1>
<input type="text" value="" placeholder="Username" id="username" name="username" required />
<input type="password" value="" placeholder="Password" id="password" name="passwd" required />
<input type="hidden" name="submitted" id="submitted" value="yes">
<button type="submit" name="submit" class="btn btn-primary btn-block btn-large">Login</button>
</form>
<button>Back</button>
</div>
<!-- Code Ends -->
Basically try this:
.info {position: absolute; }
1st Remove echo $error in the Code:
$error = '<div class="info" style="color: red; text-align: center;">You have entered an invalid username or password</div>';
//echo $error;
2nd Add this Code at the top of form tag in html
<?php
if(isset($error) && isset($_POST['submit'])){
echo $error;
}
?>
<?php
/*********************************************************************
* This script has been released with the aim that it will be useful.
* Written by Vasplus Programming Blog
* Website: www.vasplus.info
* Email: info#vasplus.info
* All Copy Rights Reserved by Vasplus Programming Blog
***********************************************************************/
session_start();
ob_start();
//Include the database connection file
include "database_connection.php";
//Check to see if the submit button has been clicked to process data
if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
{
//Variables Assignment
$username = trim(strip_tags($_POST['username']));
$user_password = trim(strip_tags($_POST['passwd']));
$validate_user_information = mysql_query("select * from `signup_and_login_users_table` where `username` = '".mysql_real_escape_string($username)."' and `password` = '".mysql_real_escape_string($user_password)."'");
//Validate against empty fields
if($username == "" || $user_password == "")
{
$error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
}
elseif(mysql_num_rows($validate_user_information) == 1) //Check if the information of the user are valid or not
{
//The submitted info of the user are valid therefore, grant the user access to the system by creating a valid session for this user and redirect this user to the welcome page
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $username;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["fullname"]);
header("location: home.php");
}
else
{
//The submitted info the user are invalid therefore, display an error message on the screen to the user
$error = '<div class="info" style="color: red; text-align: center;">You have entered an invalid username or password</div>';
//echo $error;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="img/favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CORE INTRANET</title>
<link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet">
<!-- Required header file -->
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<style>
body {
background-image: url(img/hero.jpg);
background-size: cover;
font-family: Montserrat;
}
.logo {
width: 400px;
height: 200px;
background-image: url(img/corelogo.png);
background-size: cover;
margin: -20px auto;
}
.login-block {
width: 320px;
padding: 20px;
background: transparent;
border-radius: 5px;
border-top: 5px solid #011f4b;
margin: 0 auto;
font-family: Questrial;
}
.login-block h1 {
text-align: center;
color: #000;
font-size: 18px;
text-transform: capitalize;
letter-spacing: 2px;
margin-top: 0;
margin-bottom: 20px;
}
.login-block input {
width: 100%;
height: 42px;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #ccc;
margin-bottom: 20px;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
padding: 0 20px 0 50px;
outline: none;
}
.login-block input#username {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#username:focus {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input#password {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#password:focus {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input:active, .login-block input:focus {
border: 1px solid #011f4b;
}
.login-block button {
width: 100%;
height: 40px;
background: #011f4b;
box-sizing: border-box;
border-radius: 5px;
border: 0px solid #1293e1;
color: #fff;
font-weight: 500;
text-transform: uppercase;
font-size: 14px;
font-family: Questrial;
letter-spacing: 2px;
outline: none;
cursor: pointer;
margin-bottom: 10px;
}
.login-block button:hover {
background: #1293e1;
}
</style>
<body>
<center>
<div style="font-family:Questrial, sans-serif; font-size:24px;"></div><br clear="all" /><br clear="all" />
<!-- Code Begins -->
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<div class="logo"></div>
<div class="login-block">
<?php
if(isset($error) && isset($_POST['submit'])){
echo $error;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Employee Login</h1>
<input type="text" value="" placeholder="Username" id="username" name="username" required />
<input type="password" value="" placeholder="Password" id="password" name="passwd" required />
<input type="hidden" name="submitted" id="submitted" value="yes">
<button type="submit" name="submit" class="btn btn-primary btn-block btn-large">Login</button>
</form>
<button>Back</button>
</div>
<!-- Code Ends -->
</center>
</body>
</html>

login form won't get information from SQL database and store it in a session variable

i can not get my Login script to login... i have an index.php with a register form and a login form, the register form works perfectly, but it seems like the login form does not get the information from the database when you enter the "login" button, when logging in you is redirectet to "home.php" which wil show your username with help of sessions. but i get this error "Notice: Undefined variable: username in home.php on line 12"... I think its because its not logging in and the session gets an undefined variabel. I just cant find where the problem is
i have a database named "thesozializer"
and the sql for the table is:
CREATE TABLE IF NOT EXISTS users (
id int(11) NOT NULL,
username varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
password varchar(32) NOT NULL,
sign_up_date date NOT NULL,
activated enum('0','1') NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
index.php looks like this:
<?php
mysql_connect("localhost","root","") or die("couldn't connect to database.");
mysql_select_db("thesocializer") or die("couldn't select database");
$reg = #$_POST['reg'];
//declaring variables to prevent errors
$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pswd = ""; //Password
$pswd2 = ""; // Password 2
$d = ""; // Sign up Date
$u_check = ""; // Check if username exists
//registration form
$fn = strip_tags(#$_POST['fname']);
$ln = strip_tags(#$_POST['lname']);
$un = strip_tags(#$_POST['username']);
$em = strip_tags(#$_POST['email']);
$em2 = strip_tags(#$_POST['email2']);
$pswd = strip_tags(#$_POST['password']);
$pswd2 = strip_tags(#$_POST['password2']);
$d = date("Y-m-d"); // Year - month - day
if ($reg) {
if ($em==$em2) {
// Check if user already exists
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
// Count the amount of rows where username = $un
$check = mysql_num_rows($u_check);
//Check whether Email already exists in the database
$e_check = mysql_query("SELECT email FROM users WHERE email='$em'");
//Count the number of rows returned
$email_check = mysql_num_rows($e_check);
if ($check == 0) {
if ($email_check == 0) {
//check all of the fields have been filed in
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
// check that passwords match
if ($pswd==$pswd2) {
// check the maximum length of username/first name/last name does not exceed 25 characters
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "maximum length of username/first name/last name is 25 characters!";
}
else
{
// check the maximum length of password does not exceed 25 characters and is not less than 5 characters
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "Password must be between 5 and 25 characters!";
}
else
{
//encrypt password and password 2 using md5 before sending to database
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
die("<h2>welcome to The Socializer!</h2>Login to get started");
}
}
}
else {
echo "your passwords is incorrect";
}
}
else
{
echo "fill in all fields";
}
}
else
{
echo "email already in use";
}
}
else
{
echo "username already in use";
}
}
else {
echo "The emails is not alike!";
}
}
//User Login Code
if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]);
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password_login"]);
$password_login_md5 = md5($password_login);
$sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND password='$password_login_md5' LIMIT 1");
//Check for their existance
$userCount = mysql_num_rows($sql); //Count the number of rows returned
if ($userCount == 1) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["user_login"] = $user_login;
header("location: home.php");
exit();
}
else {
echo 'username or password is incorrect';
exit();
}
}
session_start();
if (!isset($_SESSION["user_login"])) {
}
else
{
$username = $_SESSION["user_login"];
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#registrer-deg").click(function(){
$("#registrerdeg").show();
});
$("#registrer-deg").click(function(){
$("#logginn").hide();
});
$("#logg-inn").click(function(){
$("#logginn").show();
});
$("#logg-inn").click(function(){
$("#registrerdeg").hide();
});
});
</script>
<link rel="stylesheet" type="text/css" href="main.css"/>
<title>The Socializer</title>
</head>
<body>
<div id="sidebarLeft">
<div id="logo"></div>
<ul>
<li>Login</li>
<li>Register</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
<div id="timeline">
<div id="registrering">
<form id="registrerdeg" action="index.php" method="POST" style="display: none;">
<input type="text" name="fname" size="10" placeholder="First name"><br/>
<input type="text" name="lname" size="10" placeholder="Last name"><br/>
<input type="text" name="username" size="10" placeholder="Username"><br/>
<input type="text" name="email" size="10" placeholder="Email"><br/>
<input type="text" name="email2" size="10" placeholder="Confirm email"><br/>
<input type="text" name="password" size="10" placeholder="Password"><br/>
<input type="text" name="password2" size="10" placeholder="Confirm Password"><br/>
<input type="submit" name="reg" value="Registrer!">
</form>
</div>
<div id="logg_inn">
<form id="logginn" action="index.php" method="POST" style="display: none;">
<input type="text" name="user_login" size="10" placeholder="Username"><br/>
<input type="text" name="password_login" size="10" placeholder="Password"><br/>
<input type="submit" name="login" value="Logg inn!">
</form>
</div>
</div>
</body>
</html>
* {
background-color: #2C3E50;
font-family: Arial, Helvetica, Sans-serif;
font-size: 16px;
color: #AFEEEE;
}
#sidebarLeft {
width: 220px;
height: 550px;
top: 0;
left: 0;
margin-top: 50px;
margin-left: 0px;
margin-bottom: 50px;
position: fixed;
}
#sidebarRight {
width: 220px;
height: 550px;
right: 0;
top: 0;
margin-top: 50px;
margin-right: 0px;
margin-bottom: 50px;
position: fixed;
}
ul {
width: 220px;
list-style-type: none;
margin: 0px;
padding: 0;
margin-top: 30px;
}
li {
height: 35px;
width: 220px;
list-style-type: none;
margin: 5px;
}
#logo {
width: 150px;
height: 150px;
background-image: url("../img/logo.png");
-moz-border-radius: 75px;
-webkit-border-radius: 750px;
border-radius: 75px;
margin-left: 35px;
margin-top: 25px;
}
#sidebarLeft ul li a {
display: block;
width: 60px;
width: 220px;
height: 16px;
text-align: center;
margin-top: 9px;
text-decoration: none;
color: #AFEEEE;
}
#timeline {
width: 780px;
height: 550px;
margin-top: 50px;
margin-left: 240px;
top: 0;
}
input[type="text"] {
background-color: #FFFFFF;
border: 1px solid #E2E2E2;
color: #000000;
font-size: 15px;
font-weight: bold;
padding: 5px;
width: 200px;
height: 12px;
margin-bottom: 3px;
margin-top: 3px;
outline: none;
}
::-webkit-input-placeholder {
font-weight: normal;
}
:-moz-input-placeholder {
font-weight: normal;
}
::-moz-input-placeholder {
font-weight: normal;
}
:-ms-input-placeholder {
font-weight: normal;
}
input[type="submit"] {
border-top: 1px solid #96d1f8;
background: #61a6d4;
background: -webkit-gradient(linear, left top, left bottom, from(#316c94), to(#61a6d4));
background: -webkit-linear-gradient(top, #316c94, #61a6d4);
background: -moz-linear-gradient(top, #316c94, #61a6d4);
background: -ms-linear-gradient(top, #316c94, #61a6d4);
background: -o-linear-gradient(top, #316c94, #61a6d4);
padding: 5px 10px;
-webkit-border-radius: 7px;
-moz-border-radius: 7px;
border-radius: 7px;
-webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
-moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
box-shadow: rgba(0,0,0,1) 0 1px 0;
text-shadow: rgba(0,0,0,.4) 0 1px 0;
color: #ffffff;
font-size: 12px;
font-family: Helvetica, Arial, Sans-Serif;
text-decoration: none;
vertical-align: middle;
}
input[type="submit"]:hover {
border-top-color: #49718c;
background: #49718c;
color: #ccc;
}
input[type="submit"]:active {
border-top-color: #1b435e;
background: #1b435e;
}
and home.php looks like this:
<?php
mysql_connect("localhost","root","") or die("couldn't connect to database.");
mysql_select_db("thesocializer") or die("couldn't select database");
session_start();
if (!isset($_SESSION["user_login"])) {
}
else
{
$username = $_SESSION["user_login"];
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<title>The Socializer</title>
</head>
<body>
<div id="sidebarLeft">
<div id="logo">
</div>
<ul>
<li>Logg inn</li>
<li>Registrer deg</li>
<li>Om</li>
<li>Kontakt</li>
</ul>
</div>
<div id="timeline">
<?php echo "Hello, ".$username; ?>
</div>
</body>
</html>
* {
background-color: #2C3E50;
font-family: Arial, Helvetica, Sans-serif;
font-size: 16px;
color: #AFEEEE;
}
#sidebarLeft {
width: 220px;
height: 550px;
top: 0;
left: 0;
margin-top: 50px;
margin-left: 0px;
margin-bottom: 50px;
position: fixed;
}
#sidebarRight {
width: 220px;
height: 550px;
right: 0;
top: 0;
margin-top: 50px;
margin-right: 0px;
margin-bottom: 50px;
position: fixed;
}
ul {
width: 220px;
list-style-type: none;
margin: 0px;
padding: 0;
margin-top: 30px;
}
li {
height: 35px;
width: 220px;
list-style-type: none;
margin: 5px;
}
#logo {
width: 150px;
height: 150px;
background-image: url("../img/logo.png");
-moz-border-radius: 75px;
-webkit-border-radius: 750px;
border-radius: 75px;
margin-left: 35px;
margin-top: 25px;
}
#sidebarLeft ul li a {
display: block;
width: 60px;
width: 220px;
height: 16px;
text-align: center;
margin-top: 9px;
text-decoration: none;
color: #AFEEEE;
}
#timeline {
width: 780px;
height: 550px;
margin-top: 50px;
margin-left: 240px;
top: 0;
}
input[type="text"] {
background-color: #FFFFFF;
border: 1px solid #E2E2E2;
color: #000000;
font-size: 15px;
font-weight: bold;
padding: 5px;
width: 200px;
height: 12px;
margin-bottom: 3px;
margin-top: 3px;
outline: none;
}
::-webkit-input-placeholder {
font-weight: normal;
}
:-moz-input-placeholder {
font-weight: normal;
}
::-moz-input-placeholder {
font-weight: normal;
}
:-ms-input-placeholder {
font-weight: normal;
}
input[type="submit"] {
border-top: 1px solid #96d1f8;
background: #61a6d4;
background: -webkit-gradient(linear, left top, left bottom, from(#316c94), to(#61a6d4));
background: -webkit-linear-gradient(top, #316c94, #61a6d4);
background: -moz-linear-gradient(top, #316c94, #61a6d4);
background: -ms-linear-gradient(top, #316c94, #61a6d4);
background: -o-linear-gradient(top, #316c94, #61a6d4);
padding: 5px 10px;
-webkit-border-radius: 7px;
-moz-border-radius: 7px;
border-radius: 7px;
-webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
-moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
box-shadow: rgba(0,0,0,1) 0 1px 0;
text-shadow: rgba(0,0,0,.4) 0 1px 0;
color: #ffffff;
font-size: 12px;
font-family: Helvetica, Arial, Sans-Serif;
text-decoration: none;
vertical-align: middle;
}
input[type="submit"]:hover {
border-top-color: #49718c;
background: #49718c;
color: #ccc;
}
input[type="submit"]:active {
border-top-color: #1b435e;
background: #1b435e;
}
you have to move the session_start();
in both pages at the begin of your script.
index.php:
<?php
session_start();
mysql_connect("localhost","root","") or die("couldn't connect to database.");
...
home.php:
<?php
session_start();
mysql_connect("localhost","root","") or die("couldn't connect to database.");
...

Categories