In the handle_signup.php page, I try to handle with the data which was sent from the sign up page. If the nickname blank has no value, then the error message is "Please enter your nickname". So, in the signup page, I try to include the $errorMsgs array from handle_signup.php page and show the msg below the nickname input. How can I get the array from the handle_signup.php page? Currently, I use include_once, but there is no message in the array after transferring to handle_signup.php. Could some one help this issue? Thank you.
Handle the value from signup.php
<?php
require_once('./conn.php');
$errorMsgs = array('nickname'=>'', 'email'=>'', 'password'=>'');
if(isset($_POST['submit'])) {
if(empty($_POST['nickname'])) {
$errorMsgs['nickname'] = "Please enter your nickname";
header("Location: ./signup.php");
}
};
?>
Sign up page - signup.php
<?php
include_once 'handle_signup.php';
var_dump($errorMsgs);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<title>Message Board - Sign Up</title>
</head>
<body>
<div class="container_sign">
<h1 class="title">Create Account</h1>
<form class="sign" method="POST" action="./handle_signup.php">
<div>
<i class="far fa-user"></i>
<input type="text" placeholder="Nickname" name="nickname">
</div>
<p class="warning__msg"></p>
<div>
<i class="far fa-envelope"></i>
<input type="text" placeholder="Email" name="email">
</div>
<p class="warning__msg"></p>
<div>
<i class="fas fa-lock"></i>
<input type="password" placeholder="Password" name="password">
</div>
<p class="warning__msg"></p>
<input type="submit" value="SIGN UP" name="submit">
</form>
</div>
</body>
</html>
As DarkBee suggested, you can use $_SESSION for your error messages, or simply don't use header("Location: ./signup.php") but include all your PHP code in the same page.
Related
So I have been trying so much to fix this code but it's just not working. It first goes to my login page after I click on my blog which is what I want but the problem is that when I enter the login info it just brings me back to the login page as if nothing changed even if the login info is correct.
I am making a login and logout feature to a blog with PHP but without a database. I have 3 files concerning this:
This is my login.php page:
<?php
// (A) LOGIN CHECKS
require "check.php";
// (B) LOGIN PAGE HTML
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Portfolio</title>
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="portfolio_style.css" />
<link rel="stylesheet" href="login_style.css" />
</head>
<body>
<div class="container1">
<header class="header1">
<h1 class="title1">My name</h1>
<p class="subtitle"><i>Hello World!</i></p>
</header>
<section class="section1">
<nav class="nav1">
<ul>
<li>
<span class="nav2"></span>About Me
</li>
<li>
<span class="nav2"></span>Experience
</li>
<li class="active">
<span class="nav2"></span>Blog
</li>
</ul>
</nav>
<div class="container2">
<div class="content">
<?php
if (isset($failed)) { echo "<div>invalid username or password</div>"; }
?>
<button onclick="document.getElementById('id01').style.display='block'" style="width:auto;">Login</button>
<div id="id01" class="modal">
<form class="modal-content animate" method="post" target="_self">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<div class="container4">
<label for="user"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="user" required>
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit">Login</button>
</div>
</form>
</div>
<script>
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</div>
</div>
</section>
</div>
</body>
</html>
this is the file used to check and store the passes and users: check.php
<?php
// (A) START SESSION
session_start();
// (B) HANDLE LOGIN
if (isset($_POST["user"]) && !isset($_SESSION["user"])) {
// (B1) USERS & PASSWORDS - SET YOUR OWN !
$users = [
"abc" => "123",
"def" => "456",
"ghi" => "789"
];
// (B2) CHECK & VERIFY
if (isset($users[$_POST["user"]])) {
if ($users[$_POST["user"]] == $_POST["password"]) {
$_SESSION["user"] = $_POST["user"];
}
}
// (B3) FAILED LOGIN FLAG
if (!isset($_SESSION["user"])) { $failed = true; }
}
// (C) REDIRECT USER TO HOME PAGE IF SIGNED IN
if (isset($_SESSION["user"])) {
header("Location: blog.php"); // SET YOUR OWN HOME PAGE!
exit();
}
and this final one is the landing page for my blog: blog.php
<?php
session_start();
//LOGOUT
if (!isset($_POST["logout"])) {
unset($_SESSION["user"]);
}
//BACK TO LOGIN PAGE IF NOT SIGNED IN
if (!isset($_SESSION["user"]))
{
header("Location: login.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Manel's Portfolio</title>
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="portfolio_style.css" />
</head>
<body>
<div class="container1">
<header class="header1">
<h1 class="title1">My name</h1>
<p class="subtitle"><i>Hello World!</i></p>
</header>
<section class="section1">
<div class="container2">
<div class="content">
<h2 class="title2">My Blog</h2>
<p>
</p>
</div>
<!-- LOGOUT -->
<form method="post">
<input type="hidden" name="logout" value="1"/>
<input type="submit" value="logout"/>
</form>
</div>
</section>
</div>
</body>
</html>```
Your are checking not isset, so they always logout.
Change this
if (!isset($_POST["logout"])) {
unset($_SESSION["user"]);
}
to
if (isset($_POST["logout"])) {
unset($_SESSION["user"]);
}
I have the below php page, which is a holding page with a contact form:
<!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.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<!--<link rel="icon" href="../../favicon.ico">-->
<title>Coming Soon</title>
<!-- Bootstrap -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-theme.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<!-- siimple style -->
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>TEST</h1>
<h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
<h2 class="subtitle">Feel free to contact us below with any enquiries</h2>
<p>
<?php
$name = $_POST["contactname"];
?>
<form class="Contact" method="post" action="">
<label class="alignleft">Name</label>
<input type="text" name="contactname" id="contactname" placeholder="Enter Name">
<label class="alignleft">Email</label>
<input name="Email" placeholder="Email Address">
<label class="alignleft">Enquiry</label>
<textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>
<input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">
</form>
<div class="social">
<i class="fa fa-facebook" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<!--<i class="fa fa-google-plus" aria-hidden="true"></i>
<i class="fa fa-linkedin" aria-hidden="true"></i>-->
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
Within the php section I am trying to read the values of the input boxes, however even with only
$_POST["contactname"];
the website returns a 500 Internal Server Error. If i remove that line and replace with a simple:
echo "Test";
Then the site works and displays "Test"
If there something I am missing with the assigning of the variable from the input box?
You should make sure the key of the POST array is set and then validate your values first before you show them, and be sure to handle errors and invalid values!
if(isset($_POST["contactname"])){
// Do validation, like making sure its not a empty string
if (!empty($_POST["contactname"])) {
echo $_POST["contactname"];
} else {
echo "A validation error";
}
}
Also enable PHP error output which will greatly help you debug issues like this:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
And display_errors = on in your php.ini to make PHP show parse errors.
I would recommend using a second file to process the form instead of having it all on one page. Not only does it make your code look cleaner but I find it can also help with debugging. So it will look something like this:
index.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.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<!--<link rel="icon" href="../../favicon.ico">-->
<title>Coming Soon</title>
<!-- Bootstrap -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-theme.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<!-- siimple style -->
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>TEST</h1>
<h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
<h2 class="subtitle">Feel free to contact us below with any enquiries</h2>
<form class="Contact" method="post" action="form_process.php">
<label class="alignleft">Name</label>
<input type="text" name="contactname" id="contactname" placeholder="Enter Name">
<label class="alignleft">Email</label>
<input name="Email" placeholder="Email Address">
<label class="alignleft">Enquiry</label>
<textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>
<input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">
</form>
<div class="social">
<i class="fa fa-facebook" aria-hidden="true"></i>
<i class="fa fa-twitter" aria-hidden="true"></i>
<!--<i class="fa fa-google-plus" aria-hidden="true"></i>
<i class="fa fa-linkedin" aria-hidden="true"></i>-->
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
form_process.php:
<?php
if(isset($_POST['contactname'])
{
$name = $_POST['contactname'];
if(empty($name))
{
die("contact name is empty")
}
else
{
echo $name;
}
}
// Continue processing form data
?>
I would also follow the recommendations from other users about displaying PHP errors.
You should validate it first
if(isset($_POST['contactname'])){
// Do your post handling
}
Firstable , you need to check if $_POST is defined , and not empty :
if(isset($_POST["contactname"]) && !empty($_POST["contactname"])){
echo $_POST["contactname"];
}
Second , i think you need to configure you'r server to show errors and not to redirect you to 500 error:
Just modify your php.ini with this line:
display_errors = on
I cannot seem to link my login page to my home page, any help would be much appreciated. I know its connecting to the database as i can create users but for some reason its not letting me sign in with a created user.
<?php require ("insert.php"); ?>
<?php
if(isset($_POST[ 'Login' ]))
{
$email= mysqli_real_escape_string($con, $_POST ['email']);
$pswrd= mysqli_real_escape_string($con, $_POST ['pswrd']);
$result = $con->query (" select * from users where email='$email' AND pswrd='$pswrd' ");
$row = $result->fetch_array(MYSQLI_BOTH);
session_start();
$_SESSION["User ID"] = $row['UserID'];
header ('Location: home.php');
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>AMSadler login</title>
<meta charset="utf-8">
<meta name="description" content="description of webpage">
<meta name="keywords" content="keywords go here">
<meta name="author" content="Anthony">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/login.css">
<link rel="index" href="index.php">
<link rel="icon" href="img/favicon.png" sizes="16x16" type="image/png">
</head>
<body>
<div class="header">
<div id="logo">
<img src="img/logo.png" alt="logo" title="AMSadler.com"/>
</div>
<div id="signup">
<button type="button">Sign up</button>
</div>
</div>
<div id="login">
<form>
<input type="text" name="email" placeholder="Email address">
<br>
<input type="password" name="password" placeholder="Password">
<br>
<input id="Login" type="submit" name="Login" value="Login">
</form>
</div>
<footer>
<div id="copyright">
<p>© Copyright 2015</p>
</div>
</footer>
</body>
</html>
You need to change your form tags.
From,
<form>
To,
<form action="yourPageName.php" method="post">
You need to specify your method type so in your PHP code you can get it using that method. If you leave out the method part it by default uses $_GET. Seeing as your code is pointing to $_POST then you set it to method="post".
You also need to set action="" this can be set to # for same page or leave it blank or using a file name. This will redirect the form to that page.
I am trying to echo a whole html code. I am using foundation 5 and if I try to make a form variable inside the php code block, there is an error stating that the classes used in foundation 5 are not functionable. As you can see this is not a finished code so I am just looking for answers right now.
Here is my code:
<?php
session_start();
?>
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AskmanProducts</title>
<link rel="stylesheet" href="css/foundation.css" />
<script src="js/vendor/modernizr.js"></script>
<script src="js/signinvaldator.js"></script>
</head>
<body>
<div class="row" style="margin-top:10%">
<div align="center"><h2>Log In To Access This Website</h2></div>
<br />
<div class="medium-6 medium-centered large-centered large-6 columns">
<form data-abide>
<div class="name-field">
<label for="username">Username</label>
<input id="username" type="text" required="" name="user"></input>
<small class="error" data-error-message="">A username is required.</small>
</div>
<label for="password">Password</label>
<input id="password" type="password" required="" name="password"></input>
<small class="error">A Password is required.</small>
<br />
<br />
<button type="submit" name="loginbtn">Log In</button>
Sign Up
Forgot Password?
<br />
</form>
</div>
</div>
<?php
if ($_POST['loginbtn']) {
$user = $_POST['user'];
$password = $_POST['password'];
}
else
echo
?>
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
</body>
</html>
Rename your html file as php. Example: index.html -> index.php
I have created a login page and i've written the php code for it in a separate file. After login, the page should be redirected to the particular user's report. Instead, it goes to a white blank page. What could be the bug?
Login.html
<!DOCTYPE html>
<html lang="en">
<head>
<link href="css1/login_style.css" rel="stylesheet" type="text/css">
<link href="css1/font-awesome.css" rel="stylesheet" type="text/css">
<link href="css1/font-awesome.min.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="img/favicon.ico"/>
<link rel="apple-touch-icon" href="img/favicon.png"/>
</head>
<body>
<div class="logo"></div>
<div class="login"> <!-- Login -->
<h1> Login</h1>
<form class="form" method="POST" action="loginaction.php">
<p class="field">
<input type="text" name="login" placeholder="id" required/>
<i class="fa fa-user"></i>
</p>
<p class="field">
<input type="password" name="password" placeholder="Password" required/>
<i class="fa fa-lock"></i>
</p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
<p class="remember">
<input type="checkbox" id="remember" name="remember" />
<label for="remember"><span></span>Remember Me</label>
</p>
<p class="forgot">
Forgot Password?
</p>
</form>
</div> <!--/ Login-->
</body>
</html>
Below is the php code for login,
loginaction.php
<?php
if( $_SESSION["logging"]&& $_SESSION["logged"])
{
header("Location:view_restreport.php");
}
else {
if(!$_SESSION["logging"])
{
$_SESSION["logging"]=true;
header("Location:index.php");
}
else if($_SESSION["logging"])
{
$number_of_rows=checkpass();
if($number_of_rows>=1)
{
$_SESSION[user]=$_GET[userlogin];
$_SESSION['logged']=true;
header("Location:view_restreport.php");
}
}
}
function checkpass()
{
$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("konjam_disc",$conn);
$sql="select * from users where name='$_GET[userlogin]' and password='$_GET[password]'";
$result=mysql_query($sql,$conn) or die(mysql_error());
return mysql_num_rows($result);
}
?>
What could be the silly mistake that I have made? I've been trying to figure it out for quite some time but haven't been able :-(
Thanks in advance.
You don't have session_start() on your PHP page, that should always be on the top when you are dealing with sessions. Also why don't you enable error reporting on your PHP page, or at least go take a look at the error log on your web server to see what error it gives you.
Its good practice to escape php variables from queries like,
$sql="select * from users where name='".$_GET['userlogin']."' and password='".$_GET['password']."'";
Also dont forget session_start(); in the begging of your php script.
In your from you use the POST method. And in the query you use $_GET variable.
Also escaping variables is required.