Now I understand how to reverse a string in PHP:
<?php
echo strrev("Hello world!"); // outputs "!dlrow olleH"
?>
However, I have searched high and low and cannot figure out how to do what I want to do. (This is for a class project fyi)
I need to check that the password is the exact reverse of the username. So the user may choose whatever they want, but their password must be the exact reverse in order for them to proceed and log into the page.
EX:
username: me
Password: em
I do not need to store this in a file, just hard code it into the PHP script and have it check to make sure the one text field is the reverse of the other.
In theory (and I'm really new to this), I was thinking I should do something like this:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo ($_POST['password'] === strrev($_POST['username'])) ? 'true' : 'false'; // check that password is reverse of username
?>
However, this is obviously not right (or I wouldn't be here). No matter what I put it, it is allowing the user to go on to the next page.
Full code:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo ($_POST['password'] === strrev($_POST['username'])) ? 'true' : 'false'; // check that password is reverse of username
?>
<html>
<head></head>
<body>
<form action="amanot.php" method="post">
<table>
<tr>
<td><label>Username: </label></td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
<td><label>Password: </label></td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td><label>Submit</label></td>
<td><input type="submit" name="submit" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
Edit
I noticed you used my (original) answer in its entirety, but named it as an .html extension. That will not work.
Use two seperate files. One as .html for the form and the other as amanot.php for the action file.
My original answer had action="" instead of what you were using, plus there was a \ at the end of .../amanot.php\ in your file --- Try this now:
HTML form (form.html)
<html>
<head></head>
<body>
<form action="amanot.php" method="post">
<table>
<tr>
<td><label>Username: </label></td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
<td><label>Password: </label></td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td><label>Submit</label></td>
<td><input type="submit" name="submit" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
PHP (amanot.php) as a seperate file, not inside the form itself, it will not work.
<?php
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($password == strrev($username)){
echo "match"; // replace with header("Location: valid.php"); exit;
}
else{
echo "sorry"; // replace with header("Location: invalid.php"); exit;
}
} // brace for if(isset($_POST['submit']))
?>
Original answer
This works:
<?php
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($password == strrev($username)){
echo "match"; // replace with header("Location: valid.php"); exit;
}
else{
echo "sorry"; // replace with header("Location: invalid.php"); exit;
}
} // brace for if(isset($_POST['submit']))
?>
<html>
<head></head>
<body>
<form action="" method="post">
<table>
<tr>
<td><label>Username: </label></td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
<td><label>Password: </label></td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td><label>Submit</label></td>
<td><input type="submit" name="submit" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
That logic should be correct. If by 'next page' you mean the page that your PHP script is executing on, then that is the correct behavior.
If you would like to stop the current page from loading, try this instead.
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if($_POST['password'] !== strrev($_POST['username'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else {
echo 'Successful';
}
?>
Related
<?php
session_start();
include_once 'dbconnect.php';
if (isset($_SESSION['user']) != "") {
header("Location: home.php");
}
if (isset($_POST['btn-login'])) {
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res = mysql_query("SELECT * FROM telecomt_user WHERE email='$email'");
$row = mysql_fetch_array($res);
if ($row['password'] == md5($upass)) {
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
} else {
?>
<script>alert('wrong details');</script>
<?php
}
}
?>
This is my code for fetching data from database to let the user to log in by email and password. My table name is "telecomt_user".
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="email" placeholder="Your Email" required /> </td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-login">Sign In</button></td>
</tr>
<tr>
<td>Sign Up Here</td>
</tr>
</table>
</form>
And this my html form code. The code works fine in localhost but when I uploaded it to my server it does not work. It always executes this line:
<script>alert('wrong details');</script>
Is it problem in my database? But I am using the same name and pattern what I used in my localhost and also my sign up form works with the same database. My "dbconnect.php" file is also okay. What is the problem?
instead of $row['password'] , can you try to type the column of the password in the string, for example $row[0] (if password column is the first column).
I think you have to put break point in each condition like
if(your condition){
echo "something";exit;
}else{
echo "nothing";exit;
}
Better to do checking on query only email and password
$res=mysql_query("SELECT * FROM telecomt_user WHERE email='$email' AND password='md5($upass)'");
$row=mysql_fetch_array($res);
Now condition login or not
if(count($row)>=1){
//login
}else{
// credentials wrong message
}
What I would like to happen is for a user to fill out a form and that information be sent to a database. I am using html and php in dreamweaver, and WAM with phpMyAdmin.
I have tried everything and I cannot seem to get my .php file to work with my localhost test server (or any server for that matter). It is a sign up registration form, and there is a html document and a php document, I will include both:
HTML Form:
<table width="15px" border="0">
<form form action="localhost.php" method="POST">
<tr>
<td>Username</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Skype Name</td>
<td><input type="text" name="skype" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" /></td>
</tr>
<tr>
<td>Retype Password</td>
<td><input type="password" name="repass" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"/></td>
</tr>
</form>
PHP File:
<?php
$hostname= "localhost";
$database = "boost";
$username = "root";
$password = "";
$localhost = mysqli_connect($hostname, $username, $password, $database);
if(mysqli_connect_errno())
{
die('Connection Failed'.mysqli_error());
}
?>
<html>
<head>
<title>Sign Up</title>
</head>
<body>
<?php
if(isset($_POST['Submit']))
{
$username=$_Post['username'];
$skype=$_Post['skype'];
$email=$_Post['email'];
$pass=$_Post['pass'];
$repass=$_Post['repass'];
if(empty($username) || empty($skype) || empty($email) || empty($pass) || empty($repass))
{
echo "Cannot leave any field blank";
}
elseif ($pass!=$repass)
{
echo "Passwords did not match! Please try again.";
}
else
{
$sql="INSERT INTO users(Username,SkypeID,Email,Password) " .
"VALUES ('$username','$skype','$email','$pass')";
$res=mysqli_query($localhost,$sql);
if(!$res)
{
die("Query Failed" .mysqli_error($localhost));
}
else
{
echo "Welcome";
}
}
}
When I enter data into the fields to test it out, data is not sent to the localhost server (or the one connected to WAM). I don't know what I am doing wrong here. Are my HTML and PHP documents not interconnected to share values? Is there something wrong with the code? Am I not defining my database properly?
SOLUTION: My "POST" and "submit" where not being treated as case-sensitive. Thanks for the help.
Have you try to edit your php :
isset($_POST['Submit']
and change it to :
isset($_POST['submit']
variable $_POST is case-sensitive so you should use the exact same name that you assign in your html code.
CMIIW
<?php include 'header.php';
?>
<div id="body">
<form name="form4" method="get" action="" >
<table border="1" align="center">
<tr>
<td colspan="2" align="center">
<b>Login</b>
</td>
</tr>
<tr>
<td>
Email : </td>
<td><input type="email" name="txtEmail"></td>
</tr>
<tr>
<td>Passowrd : </td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td align="center"> <input name="login" type="submit" id="login" value="login"></td>
<td align="center"><input type="button" name="txtCancel" value="Cancel"/></td>
</tr>
</table>
</form>
</div>
<?php
include('connect.php');
if(isset($_REQUEST['login']))
{
$Email=$_GET['txtEmail'];
$Pwd=$_GET['txtPwd'];
$query="select * from usermaster where EmailId='$Email' and Password='$Pwd'";
echo $query;
$result=$conn->query($query);
if ($result->num_rows > 0)
{
echo "valid UserName and Password";
$_SESSION['email']=$Email;// session already started in header.php file
header("location:user/index.php");
}
else
{
echo "Error: " . $query . "<br>" . $conn->error;
//echo "Invalid UserName and Password";
}
}
?>
<?php include 'footer.php'; ?>
I have two text boxes 'username' and 'password'. But when I fill the text boxes and click on submit button it is not working and not executing php code.
When I try with empty text-box it executes php code and gives this error - invalid username password.
in your form action add # and change method to post
<form name="form4" method="post" action="#" >
and in php
if (isset($_POST['login']))
{
$email = $_POST['txtEmail'];
$pwd = $_POST['txtPwd'];
$query = "select * from usermaster where EmailId='$email' and Password='$pwd'";
$result = mysql_query($query);
$count = count($result);
if (empty($count) || $count > 1)
{
#invalid user
echo "Invalid username";
}
else
{
#valid user
echo "valid UserName and Password";
//$_SESSION['email'] = $email; // session already started in header.php file
//header("location:user/index.php")
}
}
You haven't filled the action part of your form
A good example:
<form method="get" name="form4" action="<?= $_SERVER['PHP_SELF']; ?>">
<!-- Your form here -->
</form>
Since you gave it the PHP tag this should work and redirect your form to the current file.
If you would like to use another php file:
<form method="get" name="form4" action="php_file.php">
<!-- Your form here -->
</form>
Update
A complete form example. Maybe this will help you out.
<?php
if (isset($_GET)) {
$username = isset($_GET['username']) ? $_GET['username'] : false;
$password = isset($_GET['password']) ? $_GET['password'] : false;
if (false === $useranme || false === $password) {
die("Not all fields are filled!");
}
}
?>
<form method="GET" action="<?= $_SERVER['PHP_SELF']; ?>">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="login" value="Login" />
</form>
Note:
If this doesnt help you, place the following code at the top of your file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
?>
That will turn on error reporting and show errors that might occur. If you have any errors, post them here (edit your question) so we can help you further.
i got some problem regarding form validation using PHP. I have done some research on google, the problem is I seem can't to redirect user after login using <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> in form action, it keep staying on index.php page instead going to other page.
Form
<?php
$usernameErr = $passwordErr = "";
$username = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["username"]))
{$usernameErr = "Username is required.";}
else
{$username =($_POST["username"]);}
if (empty($_POST["password"]))
{$passwordErr = "Password is required.";}
else
{$password =($_POST["password"]);}
}
?>
<body>
<form id="login" name="login" method="post" action="checklogin.php">
<table>
<tr>
<td>Username</td>
<td></td>
<td><input name="username" type="text" id="username"><span class="error"><?php echo $usernameErr;?></span></td>
</tr>
<tr>
<td>Password</td>
<td></td>
<td><input name="password" type="password" id="password"><span class="error"><?php echo $passwordErr;?></span></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="submit" value="Login"></td>
</tr>
</table>
</form>
Or do I put it on wrong page? Should I put it on checklogin.php?
Your not redirecting by echo statement.Use header() for redirection.like header("Location:index.php");
For details about header visit the link.
The form validation logic does belong to checklogin.php. You pointed your <form> to send its data to checklogin.php, but you are doing the actual validations in index.php.
ye. you need to put it in checklogin.php page.
and also make change like
if (isset($_REQUEST['submit']))
{
if (empty($_POST["username"]))
{
echo $usernameErr = "Username is required.";}
elseif (empty($_POST["password"]))
{
echo $passwordErr = "Password is required.";}
else
{
echo $password =$_POST["password"];
echo $username =$_POST["username"];}
}
?>
I know little bit of php-mysql and web programming. For my project work, i made one login module, it contains two pages but it has some errors. I use WAMP for project, whenever i feed data and press login it does not go to profile.php page and session does not start. It show same page(login.php) after Login Button Pressed From last two days i was searching for this error but i cant solve it. My project have other several pages. Here is my code.
login.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="text.css" ></link>
<title>Login</title>
</head>
<body>
<div class="container" >
<div id="header" style="text-align: center;" >
<table width="500" align="center">
<tr >
<img src="logo.jpg"/>
</tr>
</table>
</div>
<div>
<table class="form" align="center" style="height:200px;padding-left:30px;float-align:center;margin-left=20px;margin-top:40px;margin-bottom:20px;text-align:;padding-left:20px;">
<form action="loginproc.php" method="POST">
<tr>
<td align="center"colspan="780px" >
<h3 style="font-family:Calibri; font-size: 22pt;font-weight: bold; color:#3B8C8C;text-align: center;">LOGIN</h3></td>
</tr>
<tr>
<td>Email ID</td>
<td><input type="email" name="emailid" maxlength="50" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td colspan="3" align="center" style="margin-bottom:20px;">
<input type="submit" name="submit" class="button" value="Login" style="margin-bottom:20px;margin-top:20px;"/>
<input type="reset" name="reset" class="button" value="Reset" style="margin-bottom:20px;margin-top:20px;"/>
</tr>
</form></table></div>
</div>
</body>
</html>
loginproc.php
<?php
if(isset($_REQUEST['submit']))
{
$server="localhost";
$user="root";
$password="";
$db="utility";
$con = mysql_connect($server,$user,$password);
if(!$con)
{
die("Cannot Connect".mysql_error());
}
else
{
if(!mysql_select_db($db,$con))
{
header('Location: login.php');
}
else
{
$email = isset($_POST['emailid'])? $_POST['emailid']:"";
$email = mysql_real_escape_string($email);
$password = isset($_POST['pwd'])? $_POST['pwd']:"";
$password = mysql_real_escape_string($password);
$access = "SELECT * FROM register WHERE (email = '$email') AND (password = '$password')";
$access = mysql_query($access);
$accessrow = mysql_num_rows($access);
if($accessrow == 1)
{
include('session.php');
$_SESSION['email'] = $email;
header('Location: profile.php');
}
else
{
header('Location: home.php');
}
mysql_close($con);
}
}
}
?>
In your code posted, you have a leading space, which would prevent your session from starting since the session cookie has to be set before any output.
*SPACE*<?php
if(isset($_REQUEST['submit']))
{
Is that space an error in your post, or is that what you really have in your login.php file?
check the DB connection and this line if(!mysql_select_db($db,$con))
just so you know, use MySQLi or PDO_MySQL instead of MySQL as its deprecated.
Your code executed here
if(!mysql_select_db($db,$con))
{
header('Location: login.php');
}
May be the given database is not present, that's why login.php page is showing.Check your database.
And try to arrange your HTML code properly. You used Form inside Table.
It should be like this
<form>
<table>
// your code..
</table>
</form>