The following code does not work as intended, when the submit button of the form is clicked it with no data entered it goes to blog.php instead of showing the error above the form?
<?php
session_start();
include_once('connection.php');
if (isset($_SESSION['logged_in'])){
//display index
} else {
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) or empty($password)) {
$error = 'All fields are required!';
}
}
}
?>
linked with the following html form
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?> </small>
<?php } ?>
<form action="blog.php" method="post">
<input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
<input type="submit" value="login" />
</form>
If the actual validation is being done in admin.php, shouldn't the action be pointing to admin.php?
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?> </small>
<?php } ?>
<form action="admin.php" method="post">
<input type="text" name="username" placeholder="username" />
<input type="password" name="password" placeholder="password" />
<input type="submit" value="login" />
</form>
Related
I have a php code, but every time I run this code, it always displays the results in the "else" block ---> "wrong", even though the userid and password are correct, please help. Thank you.
<?php
$userid_temp="admin";
$password_temp="admin123";
if($_POST['login'])
{
$userid=$_POST['$userid'];
$password=$_POST['$password'];
if($userid==$userid_temp and $password==$password_temp)
{
echo "success";
}
else
{
echo "wrong <br />";
}
}
?>
<form method="post">
<input type="text" name="userid" placeholder="user id" /><br />
<input type="text" name="password" placeholder="password" /><br />
<input type="submit" value="login" name="login" />
</form>
<?php
$userid_temp = "admin";
$password_temp = "admin123";
if($_POST['login'])
{
$userid = $_POST['userid']; // $userid
$password = $_POST['password']; // $password
if($userid == $userid_temp and $password == $password_temp)
{
echo "success";
}
else
{
echo "wrong <br />";
}
}
?>
<form method="post">
<input type="text" name="userid" placeholder="user id" /><br />
<input type="text" name="password" placeholder="password" /><br />
<input type="submit" value="login" name="login" />
</form>
Run var_dump($_POST); die(); in the if (isset($_POST)) condition and you'll see the contents of what you are submitting.
Like #Sourabh and #ArtisticPhoenix said you're referencing a variable, but what you want is the parameter name in the $_POST array.
Drop the $ and this should work.
I am working on a web project where I want to move from one PHP page to another Php page if condition true...
In below login PHP page, I am getting username and password using $_POST[]. if both username and password got matched in (if statement) of current PHP login page then, I want to jump to another PHP page(choice.php) specified in header function below after if.
<html>
<body>
<head>
</head>
<form method="post" action="login.php">
<div id="div1">
<h1>welcome to bizdiary</h1>
<div id="div2">
<label >Username</label>
<input id="name" type="text" name="username" value=""
placeholder="username" />
<label >Password</label><input type="text" name="password" value=""
placeholder="password"/>
<input type='submit' name="login" value="login" >
</form>
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
if($username=='root' && $password=='tiger'){
header( "Location:http://localhost/bizdiary/choice.php" ); die;
}
}
?>
This code should work:
The HTML in top of the file.
Remove the action in your form.
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$host = $_SERVER['HTTP_HOST'];
// Put in here the conditional that the request need to accomplish to redirect.
if($username=='root' && $password=='tiger'){
header("Location: http://{$host}/choice.php");
}
}
?>
<html>
<body>
<head>
</head>
<body>
<form method="post">
<div id="div1">
<h1>welcome to bizdiary</h1>
<div id="div2">
<label >Username</label>
<input id="name" type="text" name="username" value="" placeholder="username" />
<label >Password</label>
<input type="text" name="password" value="" placeholder="password"/>
<input type='submit' name="login" value="login" >
</form>
</body>
</html>
You should mysql control. Example
if ($_POST){
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
if (empty($username) or empty($password)){
echo 'Don't leave blank.';
}else {
$user = mysql_query('SELECT * FROM user WHERE username = "'.$username.'" AND password = "'.$password.'"');
if (mysql_num_rows($user)){
header('Location: asd.php');
}else {
echo 'Didn't find user.';
}
}
}
How do I hide the authorization form when the user is signed in? Instead, I want the user to see: Welcome, %username%.
My code so far
<?if(!isset($_SESSION['id'])):?>
<form action="testreg.php" method="post" }>
<p>
<label><span">Username:</span></span><br>
</label>
<input name="login" type="text" size="25" maxlength="15">
</p>
<p>
<label><span class="style3">Password:</span><br>
</labe l>
<input name="password" type="password" class="style3" size="13"
maxlength="15">
</p>
<p><input name="submit" type="submit" value="Enter">
Need an account?</</p>
</form>
<?php
if (empty($_SESSION['login']) or empty($_SESSION['id']))
{
echo "You have signed in<br><a href='#'>Available only for you</a>";
}
else
{
echo "Welcome, ".$_SESSION['login']."<br><a href='link'>Profile</a>";
}
?>
<?endif?>
give your form an ID like this
<form id="loginForm" action="testreg.php" method="post">
and in your PHP where you welcome the user, throw in some inline-style.
if (empty($_SESSION['login']) or empty($_SESSION['id']))
{
echo "You have signed in<br><a href='#'>Available only for you</a>";
}
else
{
echo ' //Add this bit here
<style>
#loginForm{
display:none!important;
}
</style>
';
echo "Welcome, ".$_SESSION['login']."<br><a href='link'>Profile</a>";
}
Using this way you don't need to go on other page for signin
<?php ob_start(); ?>
<form action="testreg.php" method="post" }>
<p>
<label><span">Username:</span></span><br>
</label>
<input name="login" type="text" size="25" maxlength="15">
</p>
<p>
<label><span class="style3">Password:</span><br>
</labe l>
<input name="password" type="password" class="style3" size="13"
maxlength="15">
</p>
<p><input name="submit" type="submit" value="Enter">
Need an account?</</p>
</form>
<?php $form = ob_get_clean(); ?>
//logic goes here
if (empty($_SESSION['login']) or empty($_SESSION['id']))
{
echo $form;
}
else
{
echo "Welcome, ".$_SESSION['login']."<br><a href='link'>Profile</a>";
}
My code is below. It's working fine, but i want it not to display form again after the errors were found. How can it be done?
This is a single page, username validation program i just want to know why form is displayed again after, and how will i solve it.
<?php require'function.php'; ?>
<?php
$errors = array();
$username = "";
$password = "";
$msg = "Please Log in.";
?>
<?php if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($username != "rish" && $password != "password"){
$msg = "Try again.";
$errors['cred'] = "Wrong Credentials.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Single Page Form</title>
</head>
<body>
<?php
echo display_error($errors);
?>
<?php echo $msg ?>
<form action="singleform.php" method="post">
Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username); ?>"><br>
Password: <input type="password" name="password" value="<?php echo "" ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
You can do verifying if the $_POST data is empty. If was not empty, the users just load the page.
Can be do like code below:
<?php if(empty($_POST)):?>
<form action="singleform.php" method="post">
Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username); ?>"><br>
Password: <input type="password" name="password" value="<?php echo "" ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php endif;?>
user isset ,, if the error initializes then it won't be displayed
<? if (!isset($errors['cred'])) { ?>
<form action="singleform.php" method="post">
Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username); ?>"> <br>
Password: <input type="password" name="password" value="<?php echo "" ?>"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php } ?>
A php die function question.
when I use die(), it clean all page elements.
Is any way to echo error message and not clean all page, It looks like jump to another page when I use die() to stop code and call out the message.
Here are my code
<?PHP
$message="";
if(isset($_POST['submit'])){
$name=$_POST['name'];
$password=$_POST['password'];
//Field check
if($name && $password){$message=$name . $password;}
else{die($message="please enter name and password");}
//Check name
if($name=="alex" && $password==123){$message="Welcome ". $name;}
else{$message="wrong user or password";}
}
?>
<html>
<p>SIGN UP</p>
<form action="testing.php" method="POST">
<input type="text" name="name" placeholder="Enter Name" />
<input type="password" name="password" placeholder="Enter Password"/>
<input type="submit" name="submit" value="Sign up"/>
</form>
<div><?PHP echo $message?></div>
</html>
You should read your script for top to bottom, including anything outside of <?php ?>. When using die() your script stops then and there.
<?php $a = "something"; ?>
<html>
<p><?php echo $a?></p>
<?php die(); ?>
<p>Never here</p>
</html>
Would output
<html>
<p>something</p>
In your case do
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$password=$_POST['password'];
//Field check
if(!$name || !$password) {
$message="please enter name and password");
//Check name and password
} elseif ($name=="alex" && $password=="alex1") {
$message="Welcome ". $name;
} else {
$message="Username or password incorrect"
}
?>
<html>
<p>SIGN UP</p>
<form action="testing.php" method="POST">
<input type="text" name="name" placeholder="Enter Name" />
<input type="password" name="password" placeholder="Enter Password"/>
<input type="submit" name="submit" value="Sign up"/>
</form>
<div><?php echo $message?></div>
</html>
Also note that I'm using '==' to compare, not '='.