I'm coding php for my project and then, - php

I coded the log-in page as follows
<!DOCTYPE html5>
<html>
<head>
<? if (isset($_REQUEST['page1_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password on the first try!</h2></center>
<? } else { ?>
<center><h2>Wrong Password! Try again.
<form method="POST" action="password.php">
Password: <input type="password" NAME="pword" /><br /><br />
<input type="submit" name="page2_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
<? } elseif (isset($_REQUEST['page2_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password! But it took you two tries.</h2></center>
<? } else { ?>
<center><h2>Wrong Password Again! This was your second try.
<form method="POST" action="password.php">
Password: <input type = "password" name="pword" /><br /><br />
<input type="submit" name="page3_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
<? } elseif (isset($_REQUEST['page3_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password! But it took you three tries.</h2></center>
<? } else { ?>
<center><h2>Wrong Password Again! You're out of luck.</h2></center>
<? } ?>
<? } else { ?>
<!-- This is the first pass for the user – page 1 -->
<center><h2>Please enter your password to access this site!<br />
<form method="POST" action="password.php">
Password: <input type="password" name="pword"><br /><br />
<input type="submit" name="page1_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<? } ?>
</body>
</html>
When I run this php, everything is appearing on the same page while password function is not properly working.
I even followed the guideline that our professor provided but can't find any clear solution to this..

In PHP some times php.ini is set to not allow short code.
<? is short for <?PHP if your server is not set to read <? then it will look the way it does. Try Adding <?PHP to all <? short codes.
If the server cant read the php starts and ends then it will just run them all together.
EX:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?PHP if (isset($_REQUEST['page1_submit'])) {
if ($_REQUEST['pword'] == "meatball") { ?>
<center><h2>You entered the correct password on the first try!</h2></center>
<?PHP } else { ?>
<center><h2>Wrong Password! Try again.
<form method="POST" action="password.php">
Password: <input type="password" NAME="pword" /><br /><br />
<input type="submit" name="page2_submit" value="SUBMIT" />
<input type="reset" value="RESET" /><br />
</form>
</h2></center>
<?PHP } ?>
Notice all the <?PHP code starts
PLEASE DO NOT CODE LIKE THIS THIS IS TERRIBLE AND YOUR INSTRUCTOR NEEDS A LESSON ABOUT CLEAN CODE
http://php.net/manual/en/language.basic-syntax.phptags.php

Related

Php code gets a blank page

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;

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>";
}

How to call javascript function in php code?

on my login form, i am putting server side validations and if error occurs i want to display those error just below the validated control. Now for this, i am trying to call javascript function to show validation message in php code but not able to call.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
//here i want to call javascript function to display message
}
}
?>
<form action="login.php" method="POST">
Username <input type="text" size="30" name="txtUsername" id="user" /><br />
Password <input type="password" size="30" name="txtPassword" id="pass" /><br />
<input type="submit" value="Login" name="loginSubmit"/>
</form>
<script type="text/javascript">
function showMessage(value)
{
document.getElementById(value).innerHTML= value+"can not be empty.";
}
</script>
Please tell me how to display server side validation just below the validated control in form.
use this
if($_POST['txtUsername']=='')
{
echo '<script> showMessage("txtUsername"); </script>';
}
Something like this
<html>
<head>
<script type="text/javascript">
function showMessage(value)
{
document.getElementById(value).innerHTML= value+"can not be empty.";
}
</script>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
echo '<script> showMessage("txtUsername"); </script>';
}
}
?>
<form action="login.php" method="POST">
Username <input type="text" size="30" name="txtUsername" id="txtUsername" /><br />
Password <input type="password" size="30" name="txtPassword" id="txtPassword" /><br />
<input type="submit" value="Login" name="loginSubmit"/>
</form>
</body>
</html>
You can put your php code anywhere you like let's say in the body like attribute. You can try the following code:
<body <?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
echo "onload = 'showMessage("VALUE")'";
}
}
?> > // end of body start tag
<form action="login.php" method="POST">
Username <input type="text" size="30" name="txtUsername" id="user" /><br />
Password <input type="password" size="30" name="txtPassword" id="pass" /><br />
<input type="submit" value="Login" name="loginSubmit"/>
</form>
</body>
<script type="text/javascript">
function showMessage(value)
{
document.getElementById(value).innerHTML= value+"can not be empty.";
}
</script>
If the validation is successful the php code wont echo anything and the javascript function will not be called. Works for me :). Tell me if this helps.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
?>
<script>
//Define the function somewhere in the top or in external js and include it.
callyourfunction();
</script>
<?php
}
}
?> //Its not working

understanding sessions

I've written this very simple bit of code. but when I click log out, nothing should happen to the session, yet it is no longer displayed.
please help me understand why.
thanks.
<?php
echo <<<_END
<form method="post" action="">
<input type="hidden" name="in" value="yes" />
<input type="submit" value="Log in" /> </form>
<form method="post" action="">
<input type="hidden" name="out" value="yes" />
<input type="submit" value="Log out" /> </form>
_END;
if(isset($_POST['in']))
{
session_start();
echo "hello, logged in!";
}
if (isset($_POST['out']))
{
echo "logged out";
}
echo session_id();
?>
You can't have a session ID if you don't start the session.

Why is it still outputting the form when the session is registered?

Okay so I have a script to check to see if someone has logged in. But the login it still displays the form to login. Here is the current script:
<?php
if(session_is_registered(myusername)){
echo $_SESSION['myusername'];
echo "<br /><a class='linkText' href='logout.php'>Logout!</a></div>";
}
else{
$login_form=<<<'LOGIN'
Login<br /><br /><div class="heading_02">
<form name="form2" method="post" action="checklogin.php" style="display:inline">
<input type="text" name="myusername" id="myusername" class="user" /><br />
<input type="password" name="mypassword" id="mypassword" class="pass" /><br />
<input type="submit" name="Submit" value="Login" class="green_login_02" />
</form>
<a style="color: #A2FF00; text-decoration: none;" href="signup.php">Sign-up</a>
</div>
LOGIN;
echo $login_form;
}
?>
Just as an extra side note this part of like a clamp figuratively speaking so this is inside the first half of a clamp. The clamp syntax goes like this:
<? include("clamp1.php"); ?>
content
<? include("clamp2.php"); ?>
This appears in the first half of the clamp if that is any help.
session_start();
$_SESSION['myusername']='Ka';
function logged_in($session_name){
if (isset($session_name) and !(empty($session_name))){
return true;
}else{
return false;
}
}
if(logged_in($_SESSION['myusername'])){
echo $_SESSION['myusername'];
echo "<br /><a class='linkText' href='logout.php'>Logout!</a></div>";
}
else{
$login_form=<<<'LOGIN'
Login<br /><br /><div class="heading_02">
<form name="form2" method="post" action="checklogin.php" style="display:inline">
<input type="text" name="myusername" id="myusername" class="user" /><br />
<input type="password" name="mypassword" id="mypassword" class="pass" /><br />
<input type="submit" name="Submit" value="Login" class="green_login_02" />
</form>
<a style="color: #A2FF00; text-decoration: none;" href="signup.php">Sign-up</a>
</div>
LOGIN;
echo $login_form;
}
Works, the function session_is_registered is DEPRECATED (http://php.net/manual/en/function.session-is-registered.php). try making methods to check if a session variable is set. Don`t forget session_start()

Categories