Branching in PHP - php

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.

Related

PHP Registration Form Not Showing

This is my PHP code:
<?php
require('config.php');
if(isset($_POST['submit'])){
$email = $_POST['email'];
$email2 = $_POST['email2'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if($password == $password2){
if($email == $email2){
//All good, carry on the registration.
}else{
echo "Oh no! We can't sign you up; please double-check your passwords match.<br />";
exit();
}
}else{
echo "Oh no! We can't sign you up; please double-check your emails match.<br /><br />";
}
$form = <<<EOT
<form action="register.php" method="POST">
Username: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="password" name="password" /><br />
Confirm Password: <input type="password" name="password2" /><br />
<input type="submit" value="Play!" name="submit" />
</form>
EOT;
echo $form;
}
?>
For one reason or another, echo $form; isn't working because it's not showing the registration form.
Could someone explain how to make the content show which is between $form = <<<EOT and </form>
You have the echo $form; and the form itself within the if(isset($_POST['submit'])){ statement which means they are not going to show until you click submit which you can't because the form isn't there, move the curling brace above the form and that should do it like so:
<?php
require('config.php');
if(isset($_POST['submit'])){
$email = $_POST['email'];
$email2 = $_POST['email2'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
if($password == $password2){
if($email == $email2){
//All good, carry on the registration.
}else{
echo "Oh no! We can't sign you up; please double-check your passwords match.<br />";
exit();
}
}else{
echo "Oh no! We can't sign you up; please double-check your emails match.<br /><br />";
}
}
$form = <<<EOT
<form action="register.php" method="POST">
Username: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="password" name="password" /><br />
Confirm Password: <input type="password" name="password2" /><br />
<input type="submit" value="Play!" name="submit" />
</form>
EOT;
echo $form;
?>

Php validation error when form submit

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>

How to hide the authorization form when the user successfully signs in

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?

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 } ?>

if there is no post data redirect back to the registration page

I have a form:
<form id='register' action='http://mydev/test/register' onsubmit="return validateForm()" method='post' accept-charset='UTF-8'>
<fieldset>
<legend><br/>Create An Account</legend><br/>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<!--<label for='username' >Username*: </label>
<input type='text' name='username' id='username' maxlength="50" /><br/><br/></br/>-->
<label for='email' >Email Address*:</label>
<input type='text' name='email' id='email' maxlength="50" /><br/><br/>
<label for='password'>Password:</label>
<input type="password" name="password" id="password" value="" size="32" /><br><br>
<label for='retype_password'>Re-Enter Password:</label>
<input type="password" name="password-check" id="password-check" value="" size="32" /><br><br>
<label for='cpassword' >‌</label>
<input type="submit" value="Submit" id="submit">
</fieldset>
</form>
I than have my action page which sends an activation link to their email:
$shopper = new Shopper();
$shopper->set_email($_POST['email']);
$shopper->set_username($_POST['email']);
if($shopper->create($_POST['password']))
{
$message = "<br>User created<br>" . "An activation code has been sent<br>";
$token = $shopper->request_activation();
}
else
{
$message = "Could not create user<br>";
}
My question is... if there is no post data and they somehow end up on the register.php page.. just redirect them back to the new_registration.php page.
I know I should user header("location:../../new_registration.php");
I'm just a bit confused on how to check for the missing post data?
Should i be using empty?
I ended up using:
if (empty($_POST['email']['password'])) {
header('location:http://myjeromesdev/test/new_registration');
}
else {
$shopper = new Shopper();
$shopper->set_email($_POST['email']);
$shopper->set_username($_POST['email']);
if($shopper->create($_POST['password']))
{
$message = "<br>User created<br>" . "An activation code has been sent<br>";
$token = $shopper->request_activation();
}
else
{
$message = "Could not create user<br>";
}
}
?>
<p>
<?php
echo $message
?></p>
Yes, you can use empty, but also you should check if the received $_POST data is what you are waiting for.
if(!empty($_POST['email']) && !empty($_POST['password']))
{
$shopper = new Shopper();
$shopper->set_email($_POST['email']);
$shopper->set_username($_POST['email']);
...
}
else
{
//Your redirect
}
u can use isset and set name at submit (not set name is also ok) u can use one name of you input
see this code
<input type="submit" name="submit" value="submit"/>
if(isset($_POST['submit'])){
//code is have
}else{
header('URL');
}
if you want if all post is setted u can use AND
isset($_POST['password']) && isset($_POST['email']) and so on
hope this answer help you

Categories