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;
?>
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.
so I was making a website with a login system but my PHP will not import anything to MySQL.
Here is my code:
Registration:
<?php require_once './template/header.php'; ?>
<?
$reg = #$_POST['reg'];
//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 username
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
$check = mysql_num_rows($u_check);
// Check email
$e_check = mysql_query("SELECT email FROM users WHERE email='$em'");
$email_check = mysql_num_rows($e_check);
if ($check == 0) {
if ($email_check == 0) {
// are rows filled in
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
// passwords match?
if ($pswd==$pswd2) {
// how long are names
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "Your names are too long!";
}
else
{
// check the maximum length of password does not exceed 25 characters and is not less than 5 characters
if (strlen($pswd)>16||strlen($pswd)<6) {
echo "Passwords must be between 6 and 16 characters!";
}
else
{
//encrypt password
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')");
die("<h2>Welcome to our community!</h2>Login to your account to begin your journy...");
}
}
}
else {
echo "Your passwords don't match!";
}
}
else
{
echo "Please fill in all of the fields";
}
}
else
{
echo "Sorry, but it looks like someone has already used that email!";
}
}
else
{
echo "Username already taken ...";
}
}
else {
echo "Your E-mails don't match!";
}
}
?>
<div class="container" style="width:1000px; margin:0 auto; padding-top:25px;">
<div id="home">
<table>
<tr>
<td width="50%" valign="top">
<h2>Welcome back! Login here!</h2>
<input type="text" name="email" size="25" placeholder="E-Mail Address" /><br /><br />
<input type="password" name="pass" size="25" placeholder="Password" /><br /><br />
</td>
<td width="50%" valign="top">
<h2>Welcome! Register for free!</h2>
<form action="#" method="post">
<input type="text" size="40" name="fname" class="auto-clear" title="First Name" placeholder="First Name" value="<? echo $fn; ?>"><p />
<input type="text" size="40" name="lname" class="auto-clear" title="Last Name" placeholder="Last Name" value="<? echo $ln; ?>"><p />
<input type="text" size="40" name="username" class="auto-clear" title="Username" placeholder="Username" value="<? echo $un; ?>"><p />
<input type="text" size="40" name="email" class="auto-clear" title="Email" placeholder="Email Address" value="<? echo $em; ?>"><p />
<input type="text" size="40" name="email2" class="auto-clear" title="Repeat Email" placeholder="Email Address" value="<? echo $em2; ?>"><p />
<input type="password" size="40" name="password" value="Password ..."><p />
<input type="password" size="40" name="password2" value="Password ..."><p />
<input type="submit" name="reg" value="Sign Up!">
</form>
<!-- <select name="gender">
<option value="" disabled selected>Gender</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select><br /><br /> -->
</td>
</tr>
</table>
</div>
</div>
<?php require_once './template/footer.php'; ?>
Connection:
<?php
mysql_connect("localhost","root","");
mysql_select_db("HBN") or die("Couldn't connect to database!")
?>
Thank you guys in advance!
You need to tell SQL what table columns to add the values to. So, instead of writing
"INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')"
You want to write
"INSERT INTO users (col1,col2,col3,col4,col5...) VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0','Write something about yourself.','','','no')"
This should be printed on output browser side:
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
But there is nothing.
This is my full code, which I should have added in the first place.
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2) {
if($pass1 == $pass2) {
//All good. Nastavi broo.
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($email1);
$email2 = mysql_escape_string($email2);
$pass1 = mysql_escape_string($pass1);
$pass2 = mysql_escape_string($pass2);
}else{
echo "Sorry, your password is not corrext.";
exit();
}
}else{
echo "Sorry!";
}
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
}
?>
Your form will only appear once you click the submit button because it is set inside the if(isset($_POST['submit'])){...} conditional statement. Therefore, you need to move your last brace } above $form = <<<EOT.
} // it belongs here instead of below echo $form;
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
?>
and make sure you are accessing it as http://localhost/file.php and not as c://file.php
EDIT:
Here's a rewrite:
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
if($email1 == $email2) {
if($pass1 == $pass2) {
//All good. Nastavi broo.
$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($email1);
$email2 = mysql_escape_string($email2);
$pass1 = mysql_escape_string($pass1);
$pass2 = mysql_escape_string($pass2);
}else{
echo "Sorry, your password is not corrext.";
exit();
}
}else{
echo "Sorry!";
}
} // brace for submit conditional
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /></br>
Last Name: <input type="text" name="lname" /></br>
Username: <input type="text" name="uname" /></br>
Email: <input type="text" name="email1" /></br>
Confirm Email: <input type="text" name="email2" /></br>
Password: <input type="password" name="pass1" /></br>
Confirm Password: <input type="password" name="pass2" /></br>
<input type="submit" value="Register" name="submit" />
</form>
EOT;
echo $form;
?>
Passwords
I also noticed that you may be storing passwords in plain text. This is not recommended. If you intend on going LIVE with this, do read the following.
Use one of the following:
CRYPT_BLOWFISH
crypt()
bcrypt()
scrypt()
On OPENWALL
PBKDF2
PBKDF2 on PHP.net
PHP 5.5's password_hash() function.
Compatibility pack (if PHP < 5.5) https://github.com/ircmaxell/password_compat/
Other links:
PBKDF2 For PHP
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>
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 } ?>