Click to See Complete Forum and Search --> : Validate.


DanUK
11-11-2003, 03:31 PM
Hi there.
I am creating a form for my site which will log the user into the LAN webmail site.
I need it to make sure all the fields are completed, and that the E-mail field ends in a certain @domain.tld (to save abuse).

The PHP I have so far is :


<?php
_if_(!eregi("page.php",_$_SERVER['PHP_SELF']))_{
_____die_("Sorry, You cannot access this file directly...");
_}

_$index_=_0;
_include("header.php");
_?>

<div align="center">
<br />
<table width="100%" border="0" cellspacing="1" cellpadding="1"
align="center">
<form name="form1" action="msglist.php" method="post">
<tr><td colspan="2">Webmail Login.</td>
<tr><td align="right" width="30%"><b>Your E-mail Address</b>: &nbsp;</td>
<td><input type="text" size="10" name="f_email"
value="replace@domain.tld" style="width:140px;"></td>
<tr><td align="right" width="30%"><b>Your Username</b>: &nbsp;</td>
<td><input type="text" size="5" name="f_user" value=""
style="width:80px;"></td>
<tr><td align="right" width="30%"><b>Your Password</b>: &nbsp;</td>
<td><input type="password" size="5" name="f_user" value=""
style="width:80px;"></td>
<tr><td align="right"><b>Language</b>: &nbsp;</td><td>
<select name="lng" onChange="selectLanguage()">
<option value="0">Bulgarian
<option value="1">Czech
<option value="2">Chinese Traditional (BIG5)
<option value="3">Dutch (Netherlands)
<option value="4">Danish
<option value="5" selected>English
<option value="6">Espaņol
<option value="7">French
<option value="8">German
<option value="9">Hungarian
<option value="10">Italian
<option value="11">Icelandic
<option value="12">Norwegian
<option value="13">Polish
<option value="14">Portuguese
<option value="15">Romenian
<option value="16">Russian
<option value="17">Swedish
<option value="18">Thailand
<option value="19">Turkish
</select>
</td>
<tr><td>&nbsp;</td><td><input type="submit" name="submit"
class="mainoption" value="Login &gt;&gt;"></td></tr>
</form>
</table>
</div><br />

<?php
_include("footer.php");
?>


How can I achieve this please:

- To get the f_email field completed, and ending in a certain @domain.tld.
- To make sure the username/password are completed.

Many thanks if you can help.

pyro
11-11-2003, 04:32 PM
If you only need to check if the username and password contain data, you can do this:

if (empty($username) && empty($password)) {
echo "Please fill in your username and password.";
exit();
}And you could use something like this to be sure the email ends in the desired TLD:

if (!preg_match("/@yourdomain\\.com$/", $email)) {
echo "Please enter a valid email.";
exit();
}

DanUK
11-11-2003, 05:52 PM
thanks pyro.

I've set this as:


<?php
_if_(!eregi("page.php",_$_SERVER['PHP_SELF']))_{
_____die_("Sorry, You cannot access this file directly...");
_}

_$index_=_0;
_include("header.php");
_?>

<div align="center">
<form name="form1" action="msglist.php" method="post">
<br /><br />
<b>WebMail Login:</b> This page will log you into your Webmail.<br /><br />
<br />
<table width="75%" border="0">
<tr>
<td>Your E-mail Address:</td>
<td><input name="f_email" type="text" size="20"
value="replace@domain.tld" maxlength="80"
onfocus="if(this.value=='replace@domain.tld')this.value='@domain.tld'"
/></td>
</tr>
<tr>
<td>Your Username:</td>
<td><input name="f_user" type="text" maxlength="80" /></td>
</tr>
<tr>
<td>Your Password:</td>
<td><input name="f_pass" type="password" maxlength="80" /></td>
</tr>
<tr>
<td>Type:</td>
<td><input type="radio" name="action" value="f_option_normal" checked />
Normal.<br />
<input type="radio" name="action" value="f_option_informational" />
Informational.<br /></td>
</tr>
<tr>
<td>Language Selection:</td>
<td><select name="lng">
<option value="0">Bulgarian</option>
<option value="1">Czech</option>
<option value="2">Chinese Traditional (BIG5)</option>
<option value="3">Dutch (Netherlands)</option>
<option value="4">Danish</option>
<option value="5" selected>English</option>
<option value="6">Espaņol</option>
<option value="7">French</option>
<option value="8">German</option>
<option value="9">Hungarian</option>
<option value="10">Italian</option>
<option value="11">Icelandic</option>
<option value="12">Norwegian</option>
<option value="13">Polish</option>
<option value="14">Portuguese</option>
<option value="15">Romenian</option>
<option value="16">Russian</option>
<option value="17">Swedish</option>
<option value="18">Thailand</option>
<option value="19">Turkish</option>
</select></td>
</tr>
</table><br /><br />
<b>Your IP address:</b>_<?php_echo_$_SERVER['REMOTE_ADDR'];_?><br />
<b>Your browser information:</b>_<?php_echo_$_SERVER['HTTP_USER_AGENT'];_?>
<br /><br />
<input type="submit" class="mainoption" id="submit" value="Login" />
<input type="reset" class="mainoption" id="clear" onClick="return
confirm('Are you sure you want to clear the form?')" value="Reset the
above" /><br />
</form><br /></div>

<?php
_include("footer.php");

_if_(empty($f_user)_&&_empty($f_pass))_{

____echo_"Please fill in your username and password.";

____exit();

}

_if_(!preg_match("/@domain\.tld$/",_$f_email))_{

____echo_"Please enter a valid email.__E-mails must end in @domain.tld";

____exit();

_}
_?>


Unfortunately though, when i click submit, it's not doing any validation, and not showing the errors. Did I do something wrong? Sorry

DanUK
11-11-2003, 06:47 PM
Just tried changing bits n bobs around, and got pretty much the same.
What i'm trying to do is that if the user submits the form with username or password blank, or email blank (or if it's completed, but ends in teh wrong @domain.tld), it will echo those error messages and not submit.

DanUK
11-12-2003, 05:43 AM
I'm still trying, and trying to get this to work, with no luck! Hours on it now hehehe.
Here's what I've got:


<?php
if (!eregi("page.php", $_SERVER['PHP_SELF'])) {
die ("Sorry, You cannot access this file directly...");
}

$index = 0;
include("header.php");
header('Location: mail.php?error');
if (isset($_GET['error'])) echo 'Your submission was found to be
incomplete.';
?>

<div align="center">
<form name="form1" action="msglist.php" method="post">
<br /><br />
<b>WebMail Login:</b> This page will log you into your Webmail.<br /><br />
<br />
<table width="75%" border="0">
<tr>
<?php
if (isset($_POST['f_email']) && (! preg_match("/@yourdomainname\.org$/",
$_POST['f_email']))) {

echo "Please enter a valid e-mail. E-mails must end in
@yourdomainname.org.";

exit();

}
?>
<td>Your E-mail Address:</td>
<td><input name="f_email" type="text" size="20"
value="replace@yourdomainname.org" maxlength="80"
onfocus="if(this.value=='replace@yourdomainname.org')this.value='@yourdomain
name.org'" onblur="if(this.value=='@yourdomainname.org')
this.value='replace@yourdomainname.org'" /></td>
</tr>
<tr>
<?php
if (isset($_POST['f_user']) && (trim( $_POST['f_user'])== '')) {

echo "Please fill in your username.";

exit();

}
?>
<td>Your Username:</td>
<td><input name="f_user" type="text" maxlength="80" /></td>
</tr>
<tr>
<?php
if (isset($_POST['f_pass']) && (trim( $_POST['f_pass'])== '')) {

echo "Please fill in your password.";

exit();

}
?>
<td>Your Password:</td>
<td><input name="f_pass" type="password" maxlength="80" /></td>
</tr>
<tr>
<td>Type:</td>
<td><input type="radio" name="action" value="f_option_normal" checked />
Normal.<br />
<input type="radio" name="action" value="f_option_informational" />
Informational.<br /></td>
</tr>
<tr>
<td>Language Selection:</td>
<td><select name="lng">
<option value="0">Bulgarian</option>
<option value="1">Czech</option>
<option value="2">Chinese Traditional (BIG5)</option>
<option value="3">Dutch (Netherlands)</option>
<option value="4">Danish</option>
<option value="5" selected>English</option>
<option value="6">Espaņol</option>
<option value="7">French</option>
<option value="8">German</option>
<option value="9">Hungarian</option>
<option value="10">Italian</option>
<option value="11">Icelandic</option>
<option value="12">Norwegian</option>
<option value="13">Polish</option>
<option value="14">Portuguese</option>
<option value="15">Romenian</option>
<option value="16">Russian</option>
<option value="17">Swedish</option>
<option value="18">Thailand</option>
<option value="19">Turkish</option>
</select></td>
</tr>
</table><br /><br />
<b>Your IP address:</b> <?php echo $_SERVER['REMOTE_ADDR']; ?><br />
<b>Your browser information:</b> <?php echo $_SERVER['HTTP_USER_AGENT']; ?>
<br /><br />
<input type="submit" class="mainoption" id="submit" value="Login" />
<input type="reset" class="mainoption" id="clear" onClick="return
confirm('Are you sure you want to clear the form?')" value="Reset the
above" /><br />
</form><br /></div>

<?php
include("footer.php");
?>

pyro
11-12-2003, 07:24 AM
The validation part seems to be working correctly, but I'd question this line:

header('Location: mail.php?error');

DanUK
11-12-2003, 07:50 AM
Hi pyro, thanks for your reply.
Even with this removed, it still doesn't seem to work.

I can press "Login" and it will go to that msglist.php file, even if teh username/password is blank, or email doesn't end in that specified.

It just doesn't seem to be doing any validation. I wanted it that if the visitor clicked Login, and those fields were wrong, it would echo those errors and not submit until it was done.

Odd!!!

pyro
11-12-2003, 08:14 AM
PHP runs server-side, so it will not do any form processing until the page has been submitted. You need to move your validation to msglist.php.

DanUK
11-12-2003, 08:23 AM
Ahh.
This is a problem.

The msglist.php actually has no validation in it, and actuall resides on another server (I just removed the address so i wasn't spamming), thus I cannot edit it.

Is it possible then, that I change the "mail.php" and make it submit, to say mail2.php and have the validation in that one?

pyro
11-12-2003, 11:34 AM
Well, sortof. The problem would be passing the $_POST variables to the next page...

DanUK
11-12-2003, 03:18 PM
Hi again pyro.
This is getting confusing indeed.
I've managed to fix it a little, but now even if the fields are proper, it still sends them to mail2.php and not to the actual msglist -how confusing!
So this is what i've got:

mail.php:


<?php
if (!eregi("page.php", $_SERVER['PHP_SELF'])) {
die ("Sorry, You cannot access this file directly...");
}

$index = 0;
include("header.php");
?>

<div align="center">
<form name="form1" action="page.php?p=mail2" method="post">
<br /><br />
<b>WebMail Login:</b> This page will log you into your Webmail.<br /><br />
<br />
<table width="75%" border="0">
<tr>
<td>Your E-mail Address:</td>
<td><input name="f_email" type="text" size="20" value="replace@yourdomain.tld" maxlength="80" onfocus="if(this.value=='replace@yourdomain.tld')this.value='@yourdomain.tld'" onblur="if(this.value=='@yourdomain.tld') this.value='replace@yourdomain.tld'" /></td>
</tr>
<tr>
<td>Your Username:</td>
<td><input name="f_user" type="text" maxlength="80" /></td>
</tr>
<tr>
<td>Your Password:</td>
<td><input name="f_pass" type="password" maxlength="80" /></td>
</tr>
<tr>
<td>Type:</td>
<td><input type="radio" name="action" value="f_option_normal" checked /> Normal.<br />
<input type="radio" name="action" value="f_option_informational" /> Informational.<br /></td>
</tr>
<tr>
<td>Language Selection:</td>
<td><select name="lng">
<option value="0">Bulgarian</option>
<option value="1">Czech</option>
<option value="2">Chinese Traditional (BIG5)</option>
<option value="3">Dutch (Netherlands)</option>
<option value="4">Danish</option>
<option value="5" selected>English</option>
<option value="6">Espaņol</option>
<option value="7">French</option>
<option value="8">German</option>
<option value="9">Hungarian</option>
<option value="10">Italian</option>
<option value="11">Icelandic</option>
<option value="12">Norwegian</option>
<option value="13">Polish</option>
<option value="14">Portuguese</option>
<option value="15">Romenian</option>
<option value="16">Russian</option>
<option value="17">Swedish</option>
<option value="18">Thailand</option>
<option value="19">Turkish</option>
</select></td>
</tr>
</table><br /><br />
<b>Your IP address:</b> <?php echo $_SERVER['REMOTE_ADDR']; ?><br />
<b>Your browser information:</b> <?php echo $_SERVER['HTTP_USER_AGENT']; ?>
<br /><br />
<input type="submit" class="mainoption" id="submit" value="Login" />
<input type="reset" class="mainoption" id="clear" onClick="return confirm('Are you sure you want to clear the form?')" value="Reset the above" /><br />
</form><br /></div>

<?php
include("footer.php");
?>


mail2.php


<?php
if (!eregi("page.php", $_SERVER['PHP_SELF'])) {
die ("Sorry, You cannot access this file directly...");
}

$index = 0;
include("header.php");
if (isset($_GET['error'])) echo 'Your submission was found to be incomplete.';
?>

<div align="center">
<form name="form1" action="http://the.url/msglist.php" method="post">
<br /><br />
<b>WebMail Login:</b> This page will log you into your Webmail.<br /><br />
<br />
<?php
if (isset($_POST['f_email']) && (! preg_match("/@yourdomain\.tld$/", $_POST['f_email']))) {
echo "Please enter a valid e-mail. E-mails must end in @yourdomain.tld."; include("footer.php"); die;
}

if (isset($_POST['f_user']) && (trim($_POST['f_user'])== '')) {
echo "Please fill in your username."; include("footer.php"); die;
}

if (isset($_POST['f_pass']) && (trim($_POST['f_pass'])== '')) {
echo "Please fill in your password."; include("footer.php"); die;
}
?>
<table width="75%" border="0">
<tr>
<td>Your E-mail Address:</td>
<td><input name="f_email" type="text" size="20" value="replace@yourdomain.tld" maxlength="80" onfocus="if(this.value=='replace@yourdomain.tld')this.value='@yourdomain.tld'" onblur="if(this.value=='@yourdomain.tld') this.value='replace@yourdomain.tld'" /></td>
</tr>
<tr>
<td>Your Username:</td>
<td><input name="f_user" type="text" maxlength="80" /></td>
</tr>
<tr>
<td>Your Password:</td>
<td><input name="f_pass" type="password" maxlength="80" /></td>
</tr>
<tr>
<td>Type:</td>
<td><input type="radio" name="action" value="f_option_normal" checked /> Normal.<br />
<input type="radio" name="action" value="f_option_informational" /> Informational.<br /></td>
</tr>
<tr>
<td>Language Selection:</td>
<td><select name="lng">
<option value="0">Bulgarian</option>
<option value="1">Czech</option>
<option value="2">Chinese Traditional (BIG5)</option>
<option value="3">Dutch (Netherlands)</option>
<option value="4">Danish</option>
<option value="5" selected>English</option>
<option value="6">Espaņol</option>
<option value="7">French</option>
<option value="8">German</option>
<option value="9">Hungarian</option>
<option value="10">Italian</option>
<option value="11">Icelandic</option>
<option value="12">Norwegian</option>
<option value="13">Polish</option>
<option value="14">Portuguese</option>
<option value="15">Romenian</option>
<option value="16">Russian</option>
<option value="17">Swedish</option>
<option value="18">Thailand</option>
<option value="19">Turkish</option>
</select></td>
</tr>
</table><br /><br />
<b>Your IP address:</b> <?php echo $_SERVER['REMOTE_ADDR']; ?><br />
<b>Your browser information:</b> <?php echo $_SERVER['HTTP_USER_AGENT']; ?>
<br /><br />
<input type="submit" class="mainoption" id="submit" value="Login" />
<input type="reset" class="mainoption" id="clear" onClick="return confirm('Are you sure you want to clear the form?')" value="Reset the above" /><br />
</form><br /></div>

<?php
include("footer.php");
?>

DanUK
11-14-2003, 07:37 AM
Any ideas guys?
Any help you can give is, as always, much appreciated.

DanUK
11-14-2003, 12:30 PM
Hi.
Would this work?
I really want to get this running.

In the form itself, I would add:

<form method="post" action="validate.php" target="_self">

and the actual validate.php to look like:


<?php
if (isset($_POST['f_email']) && (! preg_match("/@yourdomainname.org$/",
$_POST['f_email']))) {

echo "Please enter a valid e-mail. E-mails must end in
@yourdomainname.org.";

exit();

}

if (isset($_POST['f_user']) && (trim( $_POST['f_user'])== '')) {

echo "Please fill in your username.";

exit();

}

if (isset($_POST['f_pass']) && (trim( $_POST['f_pass'])== '')) {

echo "Please fill in your password.";

exit();

}
?>


Would I need to add anything else? I.e. if everything was "ok" - it would submit to http://the.server.com/msglist.php?

Thanks if you can help.

pyro
11-14-2003, 12:39 PM
You're going to want to put your validation code on http://the.server.com/msglist.php.

DanUK
11-14-2003, 12:44 PM
Hi, unfortunately I can't.


Ahh.
This is a problem.

The msglist.php actually has no validation in it, and actuall resides on another server (I just removed the address so i wasn't spamming), thus I cannot edit it.

Is it possible then, that I change the "mail.php" and make it submit, to say mail2.php and have the validation in that one?

pyro
11-14-2003, 12:47 PM
Sounds like you are caught between a rock and a hard place, then. When doing server-side validation, you want to do it on the page that will also process the form, as that way your $_POST variable is still in tact.

DanUK
11-14-2003, 12:49 PM
Ah, so I should really give it up as a bad job?

pyro
11-14-2003, 12:58 PM
Well, I'd wonder why you'd be using msglist.php if you cannot edit the file... The best solution would be to use something that you can edit.

DanUK
11-14-2003, 01:20 PM
Sorry, let me explain.

We currently have a webmail server - using UebiMiau, the login page of that has absolutely no validation. It has "email, username and password".
We've wasted a lot of time and resources with people failing to fill in an email (or typing a wrong domain) resulting in clients receiving unknown "from" emails, which is terrible.

That's why I wanted to add a login page to our main website which would validate, and if all ok, send them to msglist.php which is where the webmail interface is.
(On the actual webmail index.php is the login, and that forwards to msglist.php where the messages, and suchlike are).

Hope this makes sense.

pyro
11-14-2003, 01:34 PM
Originally posted by skydan
...which would validate, and if all ok, send them to msglist.php which is where the webmail interface is.So, you wouldn't have to pass any data to msglist.php?

DanUK
11-14-2003, 07:36 PM
Not 100% sure, sorry.
There is a "live" server i just found running the same, unforunately mine is all LAN.

That URL is: http://pop3.richardp.co.uk/

HTH. Thanks.