this code is giving back a blank page in browser. Can you spot any mistake? permissions and url checked. The phpcodechecker.com says it's allright
<?php
# Check for POST login data, else set initial values
if (isset($_POST["user"])) {
$user=$_POST['user'];
$pass=hash('sha256',$_POST['pass']);
}
else {
$user="";
$pass="";
}
# Check Login Data
#
# Password is hashed (SHA256). In this case it is 'admin'.
if($user == "admin"
&& $pass == "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")
{
# Load content from local storage
include("../localstorage/content.html");
}
else
{
# Show login form. Request for username and password
echo
'<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>'
}
?>
(I downloaded it from http://www.canbike.org/information-technology/2014/02/05/php-simple-password-protection-with-session-timeout.html and i'm concious of its lack of safety)
use echo statement to print the html code as follows.
echo '<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>';
and remove the in between php tags, use only outer php tags at the beginning and end.
Shortcut open tag used but not allowed on third line up from bottom?
<?
I am late for this question but anyway i want to answer this question.
First of all, if you are new in PHP then You should change error_reporting line in your php.ini as follows:
error_reporting=E_ALL
because this setting display Parse Error instead of Black Page if there is any parse error.
Solution of your question:
There is a Syntax Error in last 'echo' statement of your script which is generating Parse Error.
You can remove this syntax error by adding semicolon(;) in last echo statement as follow:
else{
echo
'<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>';
}
?>
You can use docstring to write html in php files.
<<<EOD
<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>
EOD;
Related
I just start learning php and html and I wanted to make something that gets input and print out the output with php code. However, with the code I have, it lets me input, but not printing anything when I click submit. Why?
<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit value="Submit" />
</form>
<?php
if (isset($_POST['submit'])) { //to check if the form was submitted
$username= $_POST['username'];
echo $username;
}
?>
</body>
<html>
In the below line in your code
<input type="submit" name="submit value="Submit" />
the word name = "submit does not have a closing double colon
Your HTML is missing a quotation mark on the line where you have
<input type="submit" name="submit value="Submit" />
it should be
<input type="submit" name="submit" value="Submit" />
you missed the quotes in button name
<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit" value="Submit" />// you missed here
</form>
<?php
if (isset($_POST['submit'])) { //to check if the form was submitted
$username= $_POST['username'];
echo $username;
}
?>
</body>
<html>
There is a syntax error you have missed the double quotes name="submit"
<?php
if (isset($_POST['submit'])) { //to check if the form was submitted
// For printing whole post data
echo '<pre>'; print_r($_POST);
$username= $_POST['username'];
echo $username;
}
?>
You can also use var_dump for printing out.
Hope this will help you.
are you sure you run the page via php interpreter?
in local you can use xampp or wampp(product by windows)
in mac you can use mampp
I have written a code to get First name,last name and NIC no. If First name is missing I will show a error call "missing" infront of the textbox. All this thing is working and it will send the information in to the same page.
If first name is missing show the error in the same form which will allow user to refill it. If the user entered information correctly, I need to redirect user to another form. How can I do it?
<?php
$fnameerr="";
$name="";
if($_SERVER['REQUEST_METHOD']=="POST")
{
if(empty($_POST["fname"]))
{
$fnameerr="Missing";
}
}
?>
<head>
<title>Untitled Document</title>
</head>
<body>
<form name="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
First Name:<input type="text" name="fname" value="<?php echo htmlspecialchars($name);?>">
<span class="error" style="color:#FF0000">*<?php echo $fnameerr;?></span>
<br/><br/>
Last Name:<input type="text" name="lname" /><br/><br/>
NIC:<input type="text" name="nic" /><br/><br/>
<input type="submit" name="sub" value="Register"/>
<input type="reset" name="re" value="Reset"/>
</form>
</body>
</html>
Use header() to redirect
if(empty($_POST["fname"])) {
$fnameerr="Missing";
}
else {
header("Location: anotherForm.php");
}
I am trying to make a login page for my website, but when I press the login button, nothing happens. Help Please!
Here is the code i'm using for the login page (This is just a snippet, yes I do have the basic HTML tags)
<center><form action="login.php" method="post"></form></center>
<br>
<center><input type="text" name="username"></center>
<br>
<center><input type="password" name="password"></center>
<br>
<center><input type="submit" name="button" value="Login"></center>
This is login.php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
include ("connect.php");
if($username && $password)
{
//info is provided
}
else
{
echo "You did not provide the proper information needed for login.";
include ("signin.html");
}
?>
this is connect.php
<?php
$connect = mysql_connect("*****", "*****", "*****");
mysql_select_db("******");
?>
It's because you have a the closing </form> tag immediately after the opening tag effectively closing the form and making the rest of it invalid and inoperable. Move it to the end of your form and it will work.
Try Updated code
<center><form action="login.php" method="post">
<br>
<center><input type="text" name="username"></center>
<br>
<center><input type="password" name="password"></center>
<br>
<center><input type="submit" name="button" value="Login"></center>
</form></center>
Kindly make changes in your template.It should be like this...
<center><form action="login.php" method="post">
<br>
<center><input type="text" name="username"></center>
<br>
<center><input type="password" name="password"></center>
<br>
<center><input type="submit" name="button" value="Login"></center>
</form></center>
I have this form :
<form name="loginform" action="dologin.php" onsubmit="return isValid();" method="post">
<span id="usr">Username:</span><input type="text" style="width:230px;margin-top:10px;" name="username" />
<span id="psw">Password:</span><input type="password" style="width:230px;margin-top:10px;" name="password" />
<input type="submit" name="login" value="Login" class="button about small" style="width:100px;" />
Click to Register!
</form>
And when someone enters the wrong password it will show an error (as expected).
I have this 'if' statement, if($pass == $user['Password']).
And when it returns false it will show the error.
(Notice im only talking about the password validation, the form validation is in JS)
My question is how can I show the error? Make an empty span and then edit it (if it's possible)?
My idea.
consider login.php is your login page with login form.
<form name="loginform" action="dologin.php" onsubmit="return isValid();" method="post">
<span id="usr">Username:</span><input type="text" style="width:230px;margin-top:10px;" name="username" />
<span id="psw">Password:</span><input type="password" style="width:230px;margin-top:10px;" name="password" />
<?php if(isset($_GET['error1'])){?>
<span style='color:#f00'>Passwor or username error</span>
<?php }?>
<input type="submit" name="login" value="Login" class="button about small" style="width:100px;" />
Click to Register!
</form>
And login_sub.php your sub page for check password.
<?php
//Chech username and password
if($pass != $user['Password'])
{?>
<script>
self.location='login.php?error1=1';
</script>
<?php}
?>
By using PHP
consider you are checking the login credentials. If user is not a valid user then store the error string in the variable error.
$error="Invalid username/password";
Then you can include your login page on this page.
include "login.php";
Then on login.php page you put the below condition in a span or div
if(isset($error))
{
echo $error;
}
your work is done.
I have a log-in form on a webpage and I want users to be able to log-in or register. At the top of the page I have the PHP script with the conditional testing for which submit button was pressed. The Log-in portion works fine. The "Register" portion works (meaning, it gets into the if-statement), but it doesn't redirect to my register.php site. Any thoughts?
I've tried changing the order of the if/elseif statements but to no avail. I get no warnings from this script, but it just reloads the page rather than redirecting to register.php.
To see the actual webpage, go here.
<?php
$error = '';
session_start();
ob_start();
if (isset($_POST['register'])) {
header("http://www.mynextbit.com/authenticate/register.php");
}
elseif (isset($_POST['login'])) {
session_start();
$username = trim($_POST['username']);
$_SESSION['user'] = $username;
$password = trim($_POST['pwd']);
// location to redirect on success
$redirect = 'http://www.mynextbit.com/Pages/newtemplateTEST.php';
require_once('../includes/authenticate_mysqli.inc.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
...
And, the HTML FORM...
<?php
if ($error) {
echo "<p>$error</p>";
}
if (!$_SESSION['user']) { ?>
<font color="red"><i><userlogin>Please Log In</userlogin></i></font><br />
<form id="form1" method="post" action="" style="margin-left: 15px">
<label for="username" ><font size="-1">Username:</font></label>
<input type="text" name="username" id="username">
<br />
<label for="pwd"><font size="-1">Password:</font></label>
<input type="password" name="pwd" id="pwd">
<br />
<input name="login" type="submit" id="login" value="Log in" style="display:inline"> or
<input name="register" type="submit" id="register" value="Register" style="display:inline">
</form>
<?php } ?>
Check out here. http://php.net/manual/en/function.header.php
Use the header with the location as like this header('Location: http://www.example.com/');
header('Location:http://www.mynextbit.com/authenticate/register.php');
It should work.
Thanks
Try this:
if (isset($_POST['register'])) {
header("Location:http://www.mynextbit.com/authenticate/register.php");
}
See Reference
Perhaps, from what I see from the PHP documentation, the function HEADER must have a "location" like such?
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
That should fix your problem, I tried it and it actually redirects :P
Also (not really needed, but sometimes recommended), you may use HEREDOCS if you don't want to have too many php tag openers and closers.
Example for your code:
<?php
if ($error) {
echo "<p>$error</p>";
}
if (!$_SESSION['user']) {
echo <<<FORM
<font color="red"><i><userlogin>Please Log In</userlogin></i></font><br />
<form id="form1" method="post" action="" style="margin-left: 15px">
<label for="username" ><font size="-1">Username:</font></label>
<input type="text" name="username" id="username">
<br />
<label for="pwd"><font size="-1">Password:</font></label>
<input type="password" name="pwd" id="pwd">
<br />
<input name="login" type="submit" id="login" value="Log in" style="display:inline"> or
<input name="register" type="submit" id="register" value="Register" style="display:inline">
</form>
FORM;
}
?>