<?php
session_start();
define( “USERNAME”, “john” );
define( “PASSWORD”, “secret” );
if ( isset( $_POST[“login”] ) ) {
login();
} elseif ( isset( $_GET[“action”] ) and $_GET[“action”] == “logout” ) {
logout();
} elseif ( isset( $_SESSION[“username”] ) ) {
displayPage();
} else {
displayLoginForm();
}
function login() {
if (isset( $_POST[“username”] ) and isset( $_POST[“password”] ) ) {
if ($_POST[“username”] == USERNAME and $_POST[“password”] == PASSWORD ) {
$_SESSION[“username”] = USERNAME;
session_write_close();
header ('Location: login.php');
} else {
displayLoginForm ('Sorry, that username/password could not be found. Please try again.');
}
}
}
function logout()
{
unset( $_SESSION[“username”] );
session_write_close();
header('Location: login.php');
}
function displayPage()
{
displayPageHeader();
?>
<p> Welcome, < strong > <?php echo $_SESSION[“username”] ?>
</strong > ! You are currently logged in. < /p >
<p> <a href=”login.php?action=logout”> Logout </a> </p>

<?php
}
function displayLoginForm( $message=”” ) {
displayPageHeader();
?>
<?php
if($message) echo ‘(p class=”error”)’ $message ‘</p>’;
?>
<form action=”login.php” method=”post” >
<div style=”width: 30em;” >
<label for=”username” > Username < /label >
<input type=”text” name=”username” id=”username” value=”” / >
<label for=”password” > Password < /label >
<input type=”password” name=”password” id=”password” value=”” / >
<div style=”clear: both;” >
<input type=”submit” name=”login” value=”Login” / >
</div >
</div >
</form >
<?php
}
function displayPageHeader() {
?>
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
<head>
<title> A login/logout system < /title >
<link rel=”stylesheet” type=”text/css” href=”common.css” />
<style type=”text/css” >
.error { background: #d33; color: white; padding: 0.2em; }
</style >
< /head >
</html> 

<h1 > A login/logout system < /h1 >
<?php
}
?>

getting error at line 45 can any one tell whats wrong in it

if ($message) echo ‘<p class=”error”>’ .$message. ‘</p>’;

at line 45 if i change it to the above i get this error

Parse error: syntax error, unexpected T_CLASS, expecting ',' or ';' c:****\login.php on line 45

debug it this way

<?php
if($message)
?>
<p class="error">
<?php
$message
?>
</p>

please tell us what happened after u try this

Member Avatar for Zagga

Mispost, sorry. Please ignore.

the error is in the single quote you are using .PHP supports the character '.For both startting and ending you have to use this single quote.
Also double quote at the start and end of string must be ".

Just chnage your line to

if ($message) echo '<p class="error">' .$message. '</p>';//changed quotes

masterjiraya --> by chaniging codes according you gives errors

SCREAM: Error suppression ignored for
( ! ) Notice: Use of undefined constant “USERNAME” - assumed '“USERNAME”'
SCREAM: Error suppression ignored for
( ! ) Notice: Use of undefined constant “john” - assumed '“john”'
SCREAM: Error suppression ignored for
( ! ) Notice: Use of undefined constant “PASSWORD” - assumed '“PASSWORD”'

and a lot more

IIM ==> your code gives these error

Notice: Use of undefined constant “USERNAME” - assumed '“USERNAME”
Notice: Use of undefined constant “john” - assumed '“john”'

it just skips the scream part of the above errors...

@kakalahori- everywhere throughout your program you are using wrong quotes and double quotes.Change it.Use ' ' as double quote and " " as double quote.

What are you typing this up in, Microsoft Word? You should be using a plain text or code editor which won't replace your single and double quotes with their fancy versions.

Also, keep in mind that Notices are the lowest level of "errors" -- they won't usually break your program, though their causes may create unintended consequences.

im using komodo edit and in my editors all the quotes as ' ' "" like this its just whn i paste it here and copy it from there they changed

i m using komodo edit and ill be cheking and changing quotes.... it might b that when i copy quote from a pdf this happens

ok im not getting any error now thanks to all --- it was because of the quotes

can any one tell from the code above when i logout how do i go to main index page for login

??? im getting an error
The requested URL /Php_Sandbox/â€loginpage.php was not found on this server.

the login page is titled as loginpage.php

@kakalhori:- check if the Url above that you have written contains login.php.
This happens when you are using relative path of url but that file(login.php) is in some other location.

header ('Location: login.php');//error is because of this line.Check if login.php exist in /Php_Sandbox/ 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.