This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
Im trying to make a register system that will allow the user to make their own account, the script says that the information was sent to the database successfully, but in actual fact it's not. here is the code.
Index.php
<?php
include('login.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: home.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Network TV Login Page</title>
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body style="background:center no-repeat fixed url('https://upload.wikimedia.org/wikipedia/commons/b/b5/Melbourne_by_night.jpg'); background-size: cover">
<div id="body"> <!-- Start body div -->
<div id="nav"> <!-- Start of nav-->
<nav class="navbar navbar-default navbar-fixed-top"> <!-- Start of nav class-->
<div class="container"> <!-- Start of nav container -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Network TV</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
</div><!--/.nav-collapse -->
</div> <!-- End of nav container -->
</nav> <!-- End of nav class-->
</div> <!-- End of nav -->
<center> <!-- center login/ registration forum -->
<div id="Login"> <!-- Start login/ registration div --->
</div> <!-- End login/ registration div-->
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6">
<u>Login</u>
</div>
<div class="col-xs-6">
<u>Register</u>
</div>
</div>
<hr>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<form id="login-form" action="" method="post" role="form" style="display: block;">
<div class="form-group">
<input id="name" name="username" placeholder="Username" type="text" tabindex="1" class="form-control">
</div>
<div class="form-group">
<input id="password" name="password" placeholder="Password" type="password" tabindex="2" class="form-control">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input class="form-control btn btn-success" name="submit" type="submit" value="Log In" id="submit" tabindex="3" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<div class="text-center">
Forgot Password?
</div>
</div>
</div>
</div>
</form>
<form id="register-form" action="index.php" method="post" role="form" style="display: none;">
<div class="form-group">
<input type="text" name="newusername" id="newusername" tabindex="1" class="form-control validate-input" placeholder="Username" value="" autocomplete="off">
</div>
<div class="form-group">
<input type="email" name="newemail" id="newemail" tabindex="2" class="form-control validate-input" placeholder="Email Address" value="" autocomplete="off">
</div>
<div class="form-group">
<input type="password" name="newpassword1" id="password" tabindex="10" class="form-control" placeholder="Password" autocomplete="off">
</div>
<div class="form-group">
<input type="password" name="newpassword1" id="password" tabindex="10" class="form-control" placeholder="Password" autocomplete="off">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="register-submit" id="register-submit" tabindex="12" class="form-control btn btn-success" value="Register">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</center> <!-- Stop center -->
<div> <!-- End body div -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<style>
body {
padding-top: 90px;
}
.panel-login {
border-color: #ccc;
-webkit-box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.2);
-moz-box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.2);
box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.2);
}
.panel-login>.panel-heading {
color: #00415d;
background-color: #fff;
border-color: #fff;
text-align:center;
}
.panel-login>.panel-heading a{
text-decoration: none;
color: #666;
font-weight: bold;
font-size: 15px;
-webkit-transition: all 0.1s linear;
-moz-transition: all 0.1s linear;
transition: all 0.1s linear;
}
.panel-login>.panel-heading a.active{
color: #029f5b;
font-size: 18px;
}
.panel-login>.panel-heading hr{
margin-top: 10px;
margin-bottom: 0px;
clear: both;
border: 0;
height: 1px;
background-image: -webkit-linear-gradient(left,rgba(0, 0, 0, 0),rgba(0, 0, 0, 0.15),rgba(0, 0, 0, 0));
background-image: -moz-linear-gradient(left,rgba(0,0,0,0),rgba(0,0,0,0.15),rgba(0,0,0,0));
background-image: -ms-linear-gradient(left,rgba(0,0,0,0),rgba(0,0,0,0.15),rgba(0,0,0,0));
background-image: -o-linear-gradient(left,rgba(0,0,0,0),rgba(0,0,0,0.15),rgba(0,0,0,0));
}
.panel-login input[type="text"],.panel-login input[type="email"],.panel-login input[type="password"] {
height: 45px;
border: 1px solid #ddd;
font-size: 16px;
-webkit-transition: all 0.1s linear;
-moz-transition: all 0.1s linear;
transition: all 0.1s linear;
}
.panel-login input:hover,
.panel-login input:focus {
outline:none;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
border-color: #ccc;
}
.btn-login {
background-color: #59B2E0;
outline: none;
color: #fff;
font-size: 14px;
height: auto;
font-weight: normal;
padding: 14px 0;
text-transform: uppercase;
border-color: #59B2E6;
}
.btn-login:hover,
.btn-login:focus {
color: #fff;
background-color: #53A3CD;
border-color: #53A3CD;
}
.forgot-password {
text-decoration: underline;
color: #888;
}
.forgot-password:hover,
.forgot-password:focus {
text-decoration: underline;
color: #666;
}
.btn-register {
background-color: #1CB94E;
outline: none;
color: #fff;
font-size: 14px;
height: auto;
font-weight: normal;
padding: 14px 0;
text-transform: uppercase;
border-color: #1CB94A;
}
.btn-register:hover,
.btn-register:focus {
color: #fff;
background-color: #1CA347;
border-color: #1CA347;
}
</style>
<script>
$(function() {
$('#login-form-link').click(function(e) {
$("#login-form").delay(100).fadeIn(100);
$("#register-form").fadeOut(100);
$('#register-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
});
$('#register-form-link').click(function(e) {
$("#register-form").delay(100).fadeIn(100);
$("#login-form").fadeOut(100);
$('#login-form-link').removeClass('active');
$(this).addClass('active');
e.preventDefault();
});
});
</script>
</body>
</html>
<?php
If($_POST){
mysql_connect("localhost","root","Oliver");
mysql_select_db("users");
if(isset($_POST['register-submit'])){
$user_name = $_POST['newusername'];
$password = $_POST['newpassword1'];
$email = $_POST['newemail'];
}
$passwordmd5 = md5($password);
$query = "insert into username (username,password,email) values ('$user_name','$passwordmd5','$email')";
$query = "DELETE FROM username WHERE username = ''";
mysql_connect("localhost","root","Oliver");
mysql_select_db("videos");
$query = "DELETE FROM videos WHERE name = ''";
if (mysql_query($query)){
echo 'registration successful';
exit;
}
}
?>
Login.php
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username = $_POST['username'];
$password = md5($_POST['password']);
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("users", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = mysql_query("select * from username where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if(isset($_SESSION['login_user']))
session_destroy();
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>
I know I should upgrade the code to MySqli or PDO.
Sorry, this was getting hard to read in the comments... The below worked for me:
if(isset($_POST['register-submit'])){
$name = $_POST['newusername'];
$pass = md5($_POST['newpassword1']);
$email = $_POST['newemail'];
}
$query = "INSERT INTO `username` (`username`, `email`, `password`) VALUES ('$name', '$email', '$pass')";
Related
How do I hide my buttons "Inloggen" and "Registreren" when I am logged in? I have two seperate files; I have my index file with the menu etc and a inloggen file for the php and the form. If I am logged in I want the buttons to hide and replace it for a button with logout. How do I do that with my code?
Index:
<?php
require 'dbconnectie.php';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>PC4U</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="index.css" rel="stylesheet">
</head>
<body>
<div id="container1">
<header>
<div id="headerFotoDiv">
<img src="images/logo.jpg" height="100px" class="lihover" onclick="window.location='http://www.pc4u.hexodo.nl/'">
</div>
<div id="headerLogindiv">
<button class="btn btn-warning" onclick="window.location='?p=i'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Inloggen</button>
<button class="btn btn-warning" onclick="window.location='?p=re'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 15px; text-align: center;">Registreren</button>
</div>
</header>
<!-- Static navbar -->
<nav class="navbar navbar-default" style="width: 1000px; margin-bottom:0px;padding-bottom:0px;padding-top:0px;border:0px;border-bottom:1px solid #000;border-radius:0px; background-color:#FFFFFF;";>
<div class="container-fluid" style="padding:0px;">
<div id="navbar" class="navbar-collapse collapse" aria-expanded="true" style="height: 1px;padding:0px;">
<ul class="nav navbar-nav">
<li onclick="document.location.href='http://www.pc4u.hexodo.nl/'" class="lihover"><a>Home</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Computers
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>PC</li>
<li>Laptop</li>
</ul>
</li>
<li>Reparatie</li>
<li>Contact</li>
<li>Winkelwagen</li>
</ul>
</div>
</div>
</nav>
<!-- Vulling van de pagina -->
<div id="vulling">
<?php
if(isset($_GET['p'])) {
$pagina = $_GET['p'];
switch ($pagina) {
case "c":
include("contact.php");
break;
case "r":
include("reparatie.php");
break;
case "re":
include("registreren.php");
break;
case "i":
include("inloggen.php");
break;
case "w":
include("shoppingcart.php");
break;
case "ch":
include("computerhome.php");
break;
case "lp":
include("laptop.php");
break;
case "dp":
include("desktop.php");
break;
default:
include("home.php");
break;
}
}
else
{
include("home.php");
}
?>
</div>
</div>
</body>
</html>
Inloggen:
<?php
ob_start();
session_start();
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
//var_dump($_SESSION);
if(!isset($_POST['username']))
$_POST['username'] = '';
if(!isset($_POST['password']))
$_POST['password'] = '';
//if (isset($_SESSION['ingelogd'])&&$_SESSION ['ingelogd'] == true ) header("location: http://www.pc4u.hexodo.nl");
$dbhost = "localhost";
$dbuser = "*****";
$dbpass = "*****";
$dbname = "*****";
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) die("Connection failed");
if (isset($_POST['submit'])) {
$uname = $_POST['username'];
$wwoord = $_POST['password'];
$query = 'SELECT * FROM Klanten WHERE klant_username = "' . $uname . '" && klant_wachtwoord = "' . $wwoord . '"';
$result = $conn->query($query);
if ($result->num_rows == 1) {
$_SESSION['ingelogd'] = true;
header("location: index.php");
} else {
$_SESSION['ingelogd'] = false;
$msg = "Inloggegevens incorrect.";
}
//$conn->close();
}
?>
<link href="contact.css" rel="stylesheet">
<style type="text/css">
input, td, tr {
padding-right: 20px;
}
</style>
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-2" style="text-align: left; width: 120px; margin-left: 10px; margin-top: 10px;" for="username">Username:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" name="username" style="width: 250px; margin-top: 10px;" required placeholder="">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password" style="text-align: left; width: 120px; margin-left: 10px; margin-top: 10px;">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" style="width: 250px; margin-top: 10px;" required placeholder="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" style="margin-left: 120px; margin-bottom: 10px;" class="btn btn-default" name="submit">Inloggen</button>
</div>
</div>
</form>
Maybe try something like:
Just like in your login page check if the login session is set, if it is set show the logout button. If the login session is not set, display the 2 other buttons.
<div id="headerLogindiv">
<?php
if($_SESSION['ingelogd']){
?>
<button class="btn btn-warning" onclick="window.location=logout.php" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Uitloggen</button>
<?php
} else {
?>
<button class="btn btn-warning" onclick="window.location='?p=i'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Inloggen</button>
<button class="btn btn-warning" onclick="window.location='?p=re'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 15px; text-align: center;">Registreren</button>
}
</div>
Or you could use Jquery to check if the session has been set. First give the login / registratie button a different class then the logout button. For example: class="inloggen" for the login / registration. and class="logout" for the uitlog button. If it has been set $('.inlogen').hide() $('.uitloggen').show() visa versa
How do I hide my buttons "Inloggen" and "Registreren" when I am logged in?
You should just check if the session is set for the user or not. i.e., If the session isset
If the session is set, you should show logout button
Else if the session is not set then you should show logout and register button.
Here is what you should do
<?php
if(isset($_SESSION['ingelogd']))
{
?>
<button class="btn btn-warning" onclick="window.location=logout.php" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Uitloggen</button>
<?php
}
else
{
?>
<div id="headerLogindiv">
<button class="btn btn-warning" onclick="window.location='?p=i'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 12px; text-align: center;">Inloggen</button>
<button class="btn btn-warning" onclick="window.location='?p=re'" style="width: 130px; height:30px; margin-left: 160px; margin-top: 15px; text-align: center;">Registreren</button>
</div>
<?php
}
?>
Suggestion :
Never have the db connection in all your files, you can just have a file named as config.php or something and you can include where it is required.
Tip for debugging :
To print all the sessions
<?php
session_start();
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
?>
I have no idea what's causing the problem. I have a sign up form, and whenever I try to click the "Complete Registration" button, nothing happens! The form isn't even submitted! This is the HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, height=device-height, initial-scale = 1">
<title>Signup</title>
<link rel="icon" href="/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="/styling/signupstyle.css">
</head>
<body>
<div id="formdiv" method="post">
<h1 id="title">Sign Up Here!</h1>
<form id="form" name="signupform" onsubmit="return false;">
<div class="inputdiv">
<label for="username">Username: </label>
<input name="username" type="text" maxlength="20">
<span id="unamestatus"></span>
</div>
<div class="inputdiv">
<label for="password">Password: </label>
<input type="password" name="pass1" maxlength="20">
</div>
<div class="inputdiv">
<label for="pass2">Confirm Password: </label>
<input type="password" name="pass2" maxlength="20">
</div>
<div class="inputdiv">
<label for="email">Email: </label>
<input name="email" type="text" maxlength="100">
</div>
<div class="inputdiv">
<label for="gender">Gender: </label>
<select name="gender"">
<option value=""></option>
<option value="m">Male</option>
<option value"f">Female</option>
</select>
</div>
<div class="inputdiv">
<label for="country">Country: </label>
<select name="country">
<?php include_once("countries.html"); ?>
</select>
</div>
<div>
<input id="submit" type="submit" name="submit" value="Complete Registration!" style="width: 40%; font-size: 25px; padding: 1% 0;">
</div>
<div style="margin-top: 5%;">
<img src="/images/ConnectionLogo.PNG" alt="Connection" style="width: 25%; height: 25%;">
</div>
</form>
</div>
</body>
</html>
And this is the styling of the form:
body {
width: 100vw;
height: 100vh;
font-size: 25px;
background: linear-gradient(to right, #3DCBF2 , #3DF2D4);
overflow-x: hidden;
overflow-y: hidden;
}
#formdiv {
margin: auto;
width: 50%;
height: 85%;
border: 5px solid #2A67EB;
background-color: #37A3F0;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
#form {
width: 70%;
margin: 0 auto;
text-align: center;
height: 90%;
}
#title {
text-align: center;
color: #57E7F7;
margin-top: 1%;
}
#form input {
width: 35%;
font-size: 20px;
background-color: #70D1FA;
border: 3px solid #4187E8;
color: #376BE6;
}
#form select {
width: 25%;
font-size: 20px;
background-color: #70D1FA;
border: 3px solid #4187E8;
color: #376BE6;
}
.inputdiv {
padding-bottom: 5%;
}
#form label {
margin-right: 3%;
color: #3544F0;
font-weight: bold;
}
This is the PHP that isn't finished yet, but is certainly not causing the problem:
if ($_POST['submit']) {
// Receiving data from the form //
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
$usercheck = mysqli_query($connection, "SELECT * FROM users WHERE username='$username'");
$password = mysqli_real_escape_string($connection, $_POST['pass1']);
$passconfirm = mysqli_real_escape_string($connection, $_POST['pass2']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$emailcheck = filter_var($email, FILTER_VALIDATE_EMAIL);
$emailcheck2 = mysqli_query($connnection, "SELECT * FROM users WHERE email='$email'");
$gender = mysqli_real_escape_string($connection, $_POST['gender']);
$country = mysqli_real_escape_string($connection, $_POST['country']);
// Now for the check functions //
if ($usercheck != 0) {
die("Sorry, your username is already in use! Redirecting...");
};
if ($password != $passwordconfirm) {
die("The passwords do not match! Redirecting...");
};
if ($emailcheck != 0) {
die("That email is already in use! Redirecting...");
};
};
Because you explicitly cancel the submit action. You say onsubmit="return false;". What are you expecting it to do when you explicitly tell it to do nothing?
Also, sidenote, you have a few HTML issues:
<select name="gender"">
<option value=""></option>
<option value="m">Male</option>
<option value"f">Female</option>
should be:
<select name="gender">
<option value=""></option>
<option value="m">Male</option>
<option value="f">Female</option>
I've created this form for my website and linked the php but when i send the form i get the email but none of the info that was input into the form shows in the email, just the fields. im new to this so i apologise for the code not being the best.
Any help would be great.
<?PHP
/* Subject & Email Variables */
$emailSubject = 'Photography Quote';
$webMaster = 'ryanandelissa#seawardphotography.com';
/* Gathering Data Variables*/
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$hdyhau = $_POST['hdyhau'];
$message = $_POST['message'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Email: $email <br>
How Did You Hear About Us?: $hdyhau <br>
Message: $message <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = mail($webMaster, $emailSubject, $body, $headers);
/* Results Rendered As HTML */
$theResults = <<<EOD
<html>
<head>
<title>Contact Us</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="Contact.css">
<link href='http://fonts.googleapis.com/css?family=Lora' rel='stylesheet' type='text/css'>
<style type="text/css">
/*logo*/
img{
display: block;
margin-left: auto;
margin-right: auto;
}
/*navbar*/
#wrap{
text-align: center;
font-family: "Lora" serif;
font-size: 13px;
font-weight: normal;
padding-right: 48px;
padding-bottom: 40px;
}
.navbar li a{
display: block;
}
.navbar li{
display: inline-block;
list-style: none;
text-align: left;
}
.navbar a{
text-decoration: none;
color: #9c9c9c;
display: block;
padding: 8px;
background-color: white;
}
.navbar li ul{
position: absolute;
display: none;
margin: 0;
padding: 0;
height: auto;
}
.navbar li:hover, a:hover{
color: black;
}
.navbar li:hover ul{
display: block;
}
.navbar li ul li {
display: block;
}
h1{
text-align: center;
font-family: 'Lora' serif;
font-size: 25px;
padding-bottom: 30px;
}
input[type=button]{
border: 2px solid #9c9c9c;
background-color: white;
color: #9c9c9c;
display: block;
padding: 10px;
width: 100px;
margin-left: auto;
margin-right: auto;
}
input[type=button]:hover{
background-color: #9c9c9c;
color: white;
border: 2px #9c9c9c;
padding: 10px;
height: 41px;
}
/*footer*/
footer{
text-align: center;
font-family: "Lora" serif;
font-size: 10px;
font-weight: normal;
padding-top: 30px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12">
<img src="Images/NewLogoBlack.png" class="img-responsive" width="250px" height="250px">
</div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-12 col-md-12">
<div id="wrap">
<ul class="navbar">
<li>HOME</li>
<li>PORTFOLIO
<ul>
<li>WEDDING</li>
<li>LOVE</li>
</ul>
</li>
<li>ABOUT
<ul>
<li>US</li>
</ul>
</li>
<li>BLOG
<ul>
<li>STORIES</li>
<li>ARCHIVE</li>
</ul>
</li>
<li>CONTACT</li>
</ul>
</div>
</div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-12 col-md-12">
<h1>Thank you for your inquiry. We'll be in contact shortly.</h1>
</div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<form action="Instagram.html">
<input type="button" value="HOME">
</form>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-12 col-md-12"><footer>All images Copyright © Seaward Photography 2016. Based in San Diego, California. Available for destinations worldwide.</footer></div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
</body>
</html>
EOD;
echo($theResults);
?>
/*logo*/
img{
display: block;
margin-left: auto;
margin-right: auto;
}
/*navbar*/
#wrap{
text-align: center;
font-family: "Lora" serif;
font-size: 13px;
font-weight: normal;
padding-right: 48px;
padding-bottom: 40px;
}
.navbar li a{
display: block;
}
.navbar li{
display: inline-block;
list-style: none;
text-align: left;
}
.navbar a{
text-decoration: none;
color: #9c9c9c;
display: block;
padding: 8px;
background-color: white;
}
.navbar li ul{
position: absolute;
display: none;
margin: 0;
padding: 0;
height: auto;
}
.navbar li:hover, a:hover{
color: black;
}
.navbar li:hover ul{
display: block;
}
.navbar li ul li {
display: block;
}
/*image*/
.contact img{
width: 70%;
}
/* form */
.form label{
padding-top: 30px;
padding-left: 400px;
display: block;
float: right;
padding-right: 189px;
font-family: "Lora" serif;
font-size: 12px;
}
input, textarea{
font: 1em "Lora" sans-serif;
width:300px;
}
textarea{
vertical-align: top;
height: 5em;
resize: vertical;
}
input[type=submit]{
border: 2px solid #9c9c9c;
background-color: white;
color: #9c9c9c;
display: block;
padding: 10px;
width: 100px;
margin-left: auto;
margin-right: auto;
}
input[type=submit]:hover{
background-color: #9c9c9c;
color: white;
border: 2px #9c9c9c;
padding: 10px;
height: 44px;
}
/*footer*/
footer{
text-align: center;
font-family: "Lora" serif;
font-size: 10px;
font-weight: normal;
padding-top: 10px;
}
<!DOCTYPE html>
<html>
<head>
<title>Contact Us</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="Contact.css">
<link href='http://fonts.googleapis.com/css?family=Lora' rel='stylesheet' type='text/css'>
<style type="text/css">
img{
padding-bottom: 30px;
}
.contact-form{
padding: 20px;
}
label{
font-family: "Lora" serif;
font-weight: 300;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12">
<img src="Images/NewLogoBlack.png" class="img-responsive" width="250px" height="250px">
</div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-12 col-md-12">
<div id="wrap">
<ul class="navbar">
<li>HOME</li>
<li>PORTFOLIO
<ul>
<li>WEDDING</li>
<li>LOVE</li>
</ul>
</li>
<li>ABOUT
<ul>
<li>US</li>
</ul>
</li>
<li>BLOG
<ul>
<li>STORIES</li>
<li>ARCHIVE</li>
</ul>
</li>
<li>CONTACT</li>
</ul>
</div>
</div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<div class="col-sm-12 col-md-12">
<div class="img-list">
<img src="Images/ContactImage.jpg" class="img-responsive" width="750px" height="auto" />
</div>
</div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<form method="post" action="Contact%20Form.php" enctype="text/plain">
<div class="contact-form">
<div class="row">
<div class="col-md-6 ">
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" id="firstname">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" id="lastname">
</div>
</div>
<div class="clearfix visible-md-block"></div>
<div class="col-md-12">
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email">
</div>
<div class="clearfix visible-md-block"></div>
<div class="form-group">
<label for="hdyhau">How Did You Hear About Us?</label>
<input type="text" class="form-control" id="hdyhau">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" rows="4" id="message"></textarea>
</div>
<div class="form-group">
<div class="col-sm-8 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="SEND!" class="btn btn-primary">
</div>
</div>
</div>
</form>
<div class="col-sm-12 col-md-12"><footer>All images Copyright © Seaward Photography 2016. Based in San Diego, California. Available for destinations worldwide.</footer></div>
<div class="clearfix visible-sm-block"></div>
<div class="clearfix visible-md-block"></div>
<div class="clearfix visible-lg-block"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
</body>
</html>
Putting this in an answer so it can appear to have a resolution. ;)
Your code works for me (capitalized <?PHP and all (didn't seem to make a difference on my system)). You have some strangeness in your code (why assign your mail call to $headers?), but it seems fine, generally speaking (I even got the email). I think you have a server configuration issue.
Yes, the issue you mentioned in the comment is pretty simple, actually. In your form, your name attributes on your input elements are "firstname", "hdyhau", etc, but in your PHP you do this:
$firstnameField = !empty($_POST['firstname']) ? $_POST['firstname'] : 'n/a';
$lastnameField = $_POST['lastname'];
$emailField = $_POST['email'];
$hdyhauField = $_POST['hdyhau'];
$messageField = $_POST['messagename'];
but then reference them by their name value, not the variable you just copied them to:
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Email: $email <br>
How Did You Hear About Us?: $hdyhau <br>
Message: $message <br>
EOD;
Just change what you assign them to $firstname instead of $firstnameField OR change the variable in your $body assignment to $firstnameField, etc.
You can verify that something was actually included in the form by doing something like this before your email:
$firstname = !empty($_POST['firstname']) ? $_POST['firstname'] : 'n/a';
...
$message = !empty($_POST['message']) ? $_POST['message'] : 'n/a';
Just make sure that the value you are accessing in $_POST matches the value you used in the name="" attribute of your form.
This is a concept question. Right now I have a registration/login and a profile page system built using php and mysql, but I have one question.
I want a user to be able to make their own specific todos. So when they login they can see their todo and create their todo for them selves. Only their login can access their todo. But I don't understand how to do that in php/mysql.
Here is my login page:
<?php
session_start();
require_once('PhpConsole.phar');
require_once('connection.php');
ob_start();
require('index.php');
$data = ob_get_clean();
ob_end_clean();
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://rawgit.com/marcoceppi/bootstrap-glyphicons/master/css/bootstrap.icon-large.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="lib/sweetalert.css">
<script src="lib/sweetalert-dev.js"></script>
</head>
<body>
<div id="page">
<form method = "post" role = "form" id = "form">
<h1 style = "position:relative; left:60px; font-family: Impact;">Login </h1>
<div class="col-xs-2">
<div class="inner-addon left-addon">
<i class="glyphicon glyphicon-envelope"></i>
<input type="text" class="form-control" id = "email" name = "loginemail" placeholder="Email" />
</div>
</div>
<br>
<div class="col-xs-2">
<div class="inner-addon left-addon">
<img src = "http://i.imgur.com/GqkLI3z.png" id = "imgLock"/>
<input type="text" class="form-control" name = "loginpassword" placeholder="Password" id = "password" />
</div>
</div>
<br>
<br>
<div id = "buttons">
<div class="col-xs-2">
<div class="inner-addon left-addon">
<i class="glyphicon glyphicon-ok-sign" ></i>
<input type = "submit" class="btn btn-info" name = "loginsubmit" id = "submit"/>
</div>
<div>
</div>
</form>
</div>
<?php
if(isset($_POST["loginsubmit"])){
$loginEmail = $_POST["loginemail"];
$loginPassword = $_POST["loginpassword"];
if ($query = mysqli_query($connection, "SELECT * FROM `authe` WHERE Email = '".$loginEmail."' AND Password = '".$loginPassword."' ")) {
$rows = mysqli_num_rows($query);
if($rows>0){
echo "<script> swal('Good job!', 'Sucessfully Authenticated', 'success')</script>";
$_SESSION['email'] = $loginEmail;
$_SESSION['password'] = $loginPassword;
if(true){
// header("Location: http://localhost:8012/phpForm/Profile.php");
if ($queryTwo = mysqli_query($connection, "SELECT Username FROM `authe` WHERE Email = '".$loginEmail."'")) {
$rowsTwo = mysqli_num_rows($queryTwo);
if($rowsTwo>0){
printf($rowsTwo);
while($roww = mysqli_fetch_array($queryTwo))
{
$_SESSION["username"] = $roww['Username'];
}
}
}
echo "<script> window.location.href = 'http://localhost:8012/phpForm/Profile.php' </script>";
}
}
else {
echo "<script>sweetAlert('Oops...', 'Authentication Failed', 'error');</script>";
}
}
}
?>
</body>
</html>
<!-- <br>
<input type = "text" class="form-control" name = "loginemail" style = "width = 20px;" id = "input" placeholder = "Enter Email" required/>
<i class="glyphicon glyphicon-user form-control-feedback"></i>
<br> -->
And my profile page:
<?php
session_start();
require_once('PhpConsole.phar');
require_once('connection.php');
ob_start();
require('index.php');
$data = ob_get_clean();
ob_end_clean();
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://rawgit.com/marcoceppi/bootstrap-glyphicons/master/css/bootstrap.icon-large.css">
<link rel="stylesheet" href="main.css">
<script src="https://code.angularjs.org/1.4.9/angular.js"></script>
<link href='https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:700' rel='stylesheet' type='text/css'>
<script src = "page.js"></script>
<link rel="stylesheet" href="lib/sweetalert.css">
<script src="lib/sweetalert-dev.js"></script>
<style>
#toDoButton {
position: relative;
color: rgba(255,255,255,1);
text-decoration: none;
background-color: rgba(219,87,5,1);
font-family: 'Yanone Kaffeesatz';
font-weight: 700;
right:550px;
bottom:70px;
font-size: 3em;
display: block;
padding: 4px;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
-webkit-box-shadow: 0px 9px 0px rgba(219,31,5,1), 0px 9px 25px rgba(0,0,0,.7);
-moz-box-shadow: 0px 9px 0px rgba(219,31,5,1), 0px 9px 25px rgba(0,0,0,.7);
box-shadow: 0px 9px 0px rgba(219,31,5,1), 0px 9px 25px rgba(0,0,0,.7);
margin: 100px auto;
width: 160px;
text-align: center;
-webkit-transition: all .1s ease;
-moz-transition: all .1s ease;
-ms-transition: all .1s ease;
-o-transition: all .1s ease;
transition: all .1s ease;
}
#toDoButton:active {
-webkit-box-shadow: 0px 3px 0px rgba(219,31,5,1), 0px 3px 6px rgba(0,0,0,.9);
-moz-box-shadow: 0px 3px 0px rgba(219,31,5,1), 0px 3px 6px rgba(0,0,0,.9);
box-shadow: 0px 3px 0px rgba(219,31,5,1), 0px 3px 6px rgba(0,0,0,.9);
}
</style>
</head>
<body>
<div id="page" ng-app = "pageApp" ng-controller="pageController">
<h1>Welcome <?php echo $_SESSION['username']; ?></h1>
<h5>Here is one cool feauture. Type in the box :)</h5>
<div class = "coolFeauture" id = "CoolFeauture1">
<input type = "text" ng-model = "CoolFeauture"/>
<div ng-bind = "CoolFeauture"></div>
</div>
<div class = "todoMain">
<h4>Click on the button for a TODO App! Check it out!</h4>
Todo
</div>
</div>
<?php
?>
</body>
</html>
<!-- <br>
<input type = "text" class="form-control" name = "loginemail" style = "width = 20px;" id = "input" placeholder = "Enter Email" required/>
<i class="glyphicon glyphicon-user form-control-feedback"></i>
<br> -->
Since it is a concept question, and you mentioned that the login / register system is already done, what you need to do is:
Log them in saving a unique $_SESSION['id'], or an expirable unique key.
Additionally you may save cookies to identify their account
In the todo.php page:
Make sure that they are logged in by checking $_SESSION['id'] (if it has been set, else redirect somewhere or pop up a message).
Query their current stored TODOs by something like SELECT * FROM todo WHERE user='id'
Display a <textarea> or similar to store new TODO (which you can do by a POST to the same page).
If the page gets a POST request:
Store new TODOs by INSERT INTO todo('id', 'sanitized $_POST["text"]')
Also, always make sure to sanitize variables before doing any of these queries.
I am creating a very basic SocialSite, you know like Facebook, Twitter, etc.
And I wonder how can I output the information depending on the account logged in the 'login.php'
I have tried using on my Profile.php
$result = $mysql_query("SELECT * FROM `user` WHERE `UserName`='$user'");
while($row = mysql_fetch_array($result)){
$fname = $row['FirstName'];
}
But it only shows a lot of unidentified index and variable error.
here's my login php in-case you are wondering.
require 'Connect.php';
$username = $_POST['inputUserName'];
$password = $_POST['inputPassword'];
$sql = "SELECT * FROM 'user' WHERE 'UserName' = '$username'";
$res = mysql_query($sql) or die(mysql_error());
if( mysql_num_rows($res) > 0){
$row = mysql_fetch_assoc($res);
if($row['Password'] == $password){
$_SESSION['username'] = $username;
header('Location:../links/profilePage.php');
}
else{
$_SESSION['err_msg'] = "invalid password";
header('Location:../links/signIn.php');
}
}
else{
$sql = "SELECT * FROM 'user' WHERE `UserName` = '$username'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res)){
$row = mysql_fetch_assoc($res);
if($row['Password'] == $password){
$_SESSION['username'] = $username;
header('Location:../links/profilePage.php');
}
else{
$_SESSION['err_msg'] = "invalid password";
header('Location:../links/signIn.php');
}
}
else{
$_SESSION['err_msg'] = "User does not exist";
header('Location:../homePage.php');
}
}
Help is always appreciated. thank you.
Edit: The whole ProfilePage
<html>
<?php
session_start();
require 'Connect.php';
$user = mysql_real_escape_string($_POST['username']);
$result = mysql_query("SELECT * FROM `user` WHERE `UserName`='$user'");
while($row = mysql_fetch_array($result)){
$fname = $row['FirstName'];
}
?>
<head>
<!-- bootstrap plugin -->
<link href="../css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
<link href="../css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css" />
<link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<!-- jquery plugin -->
<script src="../js/jquery-1.11.1.js"></script>
<!-- Site body css -->
<style type="text/css">
#SiteBody{
width: 1100px;
margin: 0 auto;
}
</style>
<body>
<div id="SiteBody">
<center>
<div class="hero-unit">
<!-- Start header -->
<img src="../images/logo.png"/>
<!-- End header -->
</center>
<!--Header Bar!-->
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<a class="brand">User Profile</a>
<!--Nav bar items!-->
<p class="navbar-text pull-right">
Search: <input id="search" name="search" type="search" placeholder="search for friends" class="input-medium">
Home
Settings
Sign-Out
</p>
</div>
</div>
<!--Header Bar END !-->
<br>
<!--icon-->
<div class="well span2" style="background-color:white; height: 200px; width: 135px; border: 5px rigid; border-color: white;" >
<ul class="nav nav-list">
<img src="../images/icon.png" alt="displaypic" style="height:200px; width:300px;">
<p style = "text-align: center">
</p>
</ul>
</div>
<!-- Post Status -->
<!-- <div class="row-fluid" style="padding:1px;"> -->
<div class="well span2" style="background-color:#4CBB17; height: 200px; width: 820px; border: 5px rigid; border-color: black;" >
<ul class="nav nav-list">
<strong> *SLIDE SHOW* </strong>
</ul>
</div>
<!-- Basic Information -->
<!-- <div class="row-fluid" > -->
<div class="well span2" style="font-size: 25; background-color:#4DBD33; height:300px; width:620px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<strong><u>Basic Information</u></strong>
<br>
<br>
<br>
Name:<?php echo '$fname' ?>
<br>
<br>
Gender:
<br>
<br>
Birthday:
<br>
<br>
Address:
<br>
<br>
Email:
<br>
<br>
Contact number:
<br>
<br>
</ul>
</div>
<!-- Display Picture -->
<!-- <div class="row-fluid" style="padding:1px;"> -->
<div class="well span2" style="background-color:#55AE3A; height: 300px; width: 335px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<img src="../images/def.png" alt="displaypic" style="height:250px; width:285px; border:10px ridge; border-color:green; margin-top: 10px;">
<p style = "text-align: center">
(User Name) <br>
</p>
</ul>
</div>
<!--work and edu boutton-->
<div class="well span2" style="background-color:#93DB70; height: 250px; width: 280px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<img src="../images/button1.png" alt="displaypic" style="height:250px; width:285px; margin-top: 10px;">
<p style = "text-align: center">
</p>
</ul>
</div>
<!--hobbies and interests boutton-->
<div class="well span2" style="background-color:#4CBB17; height: 250px; width: 280px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<img src="../images/button2.png" alt="displaypic" style="height:250px; width:285px; margin-top: 10px;">
<p style = "text-align: center">
</p>
</ul>
</div>
<!--upload boutton-->
<div class="well span2" style="background-color:#93DB70; height: 250px; width: 335px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<img src="../images/button3.png" alt="displaypic" style="height:250px; width:285px; margin-top: 10px;">
<p style = "text-align: center">
</p>
</ul>
</div>
<!-- Friends List -->
<!-- <div class="row-fluid"style="padding:1px;"> -->
<div class="well span2" style="background-color:#4DBD33; height: 250px; width: 620px; border: 5px rigid; border-color: black;">
<ul class="nav nav-list">
<strong>Friends List</strong>
</ul>
</div>
<!--view boutton-->
<div class="well span2" style="background-color:#55AE3A; height: 250px; width: 335px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<img src="../images/button4.png" alt="displaypic" style="height:250px; width:285px; margin-top: 10px;">
<p style = "text-align: center">
</p>
</ul>
</div>
<div class="well span2" style="background-color:black; height: 2px; width: 1015px; border: 5px rigid; border-color: black; " >
<ul class="nav nav-list">
<p style = "text-align: center" class="navbar-link">
(c) Cabreros.Parman.Victory.Ylanan.
</p>
</ul>
</div>
</div>
</div>
</body>
$result = $mysql_query("SELECT * FROM `user` WHERE `UserName`='$user'");
Try removing the $ sign before the function mysql_query (i assume you mean the function)?
Some other tips: You should never ever pass user input values to your sql query to prevent the risk of SQL Injections. You have some code duplication here (and you do the same sql query twice, why?), maybe you should rethink your code architecture and use classes/functions.
It seems you save the passwords of your users in plain text? You should at least hash these passwords for security reasons.
You need to tell us what errors you are getting. I can suggest that one of the values does not exist such as
$_POST['inputUserName'];
$_POST['inputPassword'];
$row['Password']
You can use isset to see if it has a value
if (isset($_POST['inputUserName'])
$username = $_POST['inputUserName'];
else
$username = '';