/    Sign up×
Community /Pin to ProfileBookmark

Display Error’s on same page after Sumbit Form

Hi Guys, I am learning PHP and I have a question, I have a form called index.php basic code below:

[code=html]
<form action=”logincheck.php” method=”post”>
<table>
<tr>
<td>Username: </td>
<td><input type=”text” name=”user” value=”<?php echo $user; ?>”/></td>
</tr>
<tr>
<td>Password: </td>
<td><input type=”password” name=”password”/></td>
</tr>
<td><input type=”submit” name=”Submit” value=”Login” /></td>
<td><input type=”button” name=”Register” value=”Register” onclick=”location.href=’register.php'” /></td>
</table>
[/code]

Once I submit the page it calls logincheck.php if it encounters errors I have a include(index.php) and the displays error, my question is how would you guys go about redisplaying the errors under the table in the above index.php file?

I am currently using the following:

[code=php]
if (is_user($user,$pass,$file) == true)
{
include (‘logdin.php’);
print ‘Congrats ‘. $_POST[‘user’].’ you have managed to log in’;
} else {
include (‘index.php’);
print ‘Username and Password combination doesn’t exist’;
}
[/code]

Is it OK to use the include command like I have above or is there a better way? What about the header(), what is that used for? I know the difference between include() and require() but don’t know what header() is used for.
If I use the include command and it is a good idea how can I keep the user name in the username field once I have submitted?

I hope you know what I mean.
Thanks.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@drudekenAug 19.2010 — Put your form in its own php file.

form.php

Then you can add it where ever you need it:

index.php
[code=php]
//html header stuff


<body>

<?php

if ($loggedin == false){
include(form.php);
}
?>

</body>
[/code]



loginCheck.php
[code=php]

<?php

if($error){
include('form.php');

//print errors
}

?>
[/code]
Copy linkTweet thisAlerts:
@drudekenAug 19.2010 — [code=php]
include(form.php);

//Should be

include('form.php');

//under index.php
[/code]
Copy linkTweet thisAlerts:
@protonauthorAug 21.2010 — Thanks drudeken (if I look at your name quickly I read drunken lol)

I am trying your method, any one else have a different method?
Copy linkTweet thisAlerts:
@protonauthorAug 21.2010 — Sorry for asking another question, as far as I understand,

When $loggedin is checked it will be false first time around and load the from.php. But is there a time when it is not false? ?

Is [B]$loggedin[/B] just your example of my [B]if (is_user($user,$pass,$file) == true) [/B] or have I got it wrong? I am confused.

Thanks.
Copy linkTweet thisAlerts:
@drudekenAug 21.2010 — Yeah, however you store your session showing if a user is singed in or if the login was successful. If you are still having trouble post some more of your code and I'll take a look.
Copy linkTweet thisAlerts:
@protonauthorAug 22.2010 — Well here is my code, this is the first page
[code=php]
// the HTLM header goes here
if ($loggedin == false)
{
include('form.php');
}
// HTML footer goes here
[/code]

The this is my logincheck.php page
[code=php]
// get data passed from form
$user = $_POST['user'];
$pass = $_POST['password'];
$file = dirname(__FILE__).'/users.txt';

//set user name and password + file into sessions
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
$_SESSION['file'] = $file;

//check if user already registered
if (is_user($user,$pass,$file) == true)
{
include ('page1.php'); // page for success with menu options
print 'Congrats '. $_POST['user'].' you have managed to log in';

} else {
include ('index.php');
print 'The username '. $_POST['user'].' and password combination does not exsist, please register';

}

// check if username is in file
function is_user($user,$pass,$file)
{
$userpass = $user.':'.$pass;
if (!is_file($file))
{
return false;
}
$fp = fopen($file,"r") or die("Could not open $file");
while ($line=fgets($fp))
{
if (trim($line)==$userpass)
{
fclose($fp);
return true;
}
}
fclose($fp);
return false;
}[/code]


I am not using MySQL as I have not learnt that properly.

Here is my form.php file
[code=php]
$user = $_POST['user'];
echo "
<form action='logincheck.php' method='post'>
<table>
<tr>
<td>Username: </td>
<td><input type='text' name='$user' /></td>
</tr>
<tr>
<td>Password: </td>
<td><input type='password' name='password'/></td>
</tr>
<td><input type='submit' name='Submit' value='Login' /></td>
<td><input type='button' name='Register' value='Register' onclick='location.href="register.php"' /></td>
</table>
</form>";
[/code]
Copy linkTweet thisAlerts:
@savvykmsAug 23.2010 — I used to use login systems I made using text files or arrays defined in a PHP file included via include_once("settings.php"). I suggest not using a text file so generically named, as someone could check on a hunch and find all the information, but you could also deny access to it via placing it in an info/ directory and using a .htaccess file to require authentication.
Copy linkTweet thisAlerts:
@tirnaAug 23.2010 — 

.......any one else have a different method?[/quote]


another option you could consider is using AJAX.

When the user clicks the login button on the login page:

1) Do an optional javascript validation for username/password.

2) Then send an ajax request containing the username/password to logincheck.php where you validate the username/password and check for their existence in the database.

3) If there are any errors with the username/password, send an appropriate message or messages back to the browser and add the error message to a container (like a div or <p>) under the username/password login table on the login page.

This way, if there are any errors with the username/password, the login page will not need to refresh before the user tries to login again.

4) If the usernme/password in 3) pass user authentication, redirect the user to their home page on your website after setting a session variable(s) for them to say they are logged in.
×

Success!

Help @proton spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...