By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,948 Members | 1,522 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,948 IT Pros & Developers. It's quick & easy.

Varifying Forms Text Data Help Needed

P: n/a
I bought the PHP and MySQL For Dummies book and I'm having trouble
understanding how I use PHP to verify and check forms input text data
- the book shows snippets of code so I know how to do the actual check
but I don't understand what web page the php checking code should go
into?

For example, I have a simple login web page (username and password)
and then it calls my mainmenu php web page - I want to make a check
that a username was actually typed in *before* calling the mainmenu
php page - I also know that I can do this check using javascript but
I'd like to stay in php if I can - how can I make the check and stay
on this login form before calling the mainmenu page?

Thanks...

Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Sat, 05 Jul 2003 23:55:02 GMT, Ralph
Freshour <ra***@primemail.com> amazingly managed to produce the
following with their Etch-A-Sketch:
I bought the PHP and MySQL For Dummies book and I'm having trouble
understanding how I use PHP to verify and check forms input text
data - the book shows snippets of code so I know how to do the
actual check but I don't understand what web page the php checking
code should go into?

For example, I have a simple login web page (username and password)
and then it calls my mainmenu php web page - I want to make a check
that a username was actually typed in *before* calling the mainmenu
php page - I also know that I can do this check using javascript
but I'd like to stay in php if I can - how can I make the check and
stay on this login form before calling the mainmenu page?

Thanks...


Assuming you have 'username' and 'password' as your form text fields,
and your method is POST:
<?php
if (!preg_match("/([a-zA-Z_.-]+)/", $_POST['username'])) {
@header('Location: http://' .
$_SERVER['HTTP_HOST'] .
'/login.php'
);
}

if (!empty($_POST['password'])) {
// do check for password match
if (md5($_POST['password']) == $required_password) {
@header('Location: http://' .
$_SERVER['HTTP_HOST'] .
'/mainmenu.php'
);
} else {
die('Invalid login');
}
?>
This isn't a coplete solution as you'll need to fill out the
commented areas, but should do what you're after =)
HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPwdr92fqtj251CDhEQJZ2ACfSxH6N4JBiaHnMl8wIJxL+4 Whfy0AoIfT
8wKHusl/p2M0a6HBdp01qcEb
=g6N7
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #2

P: n/a
@header('Location: http://' .$_SERVER['HTTP_HOST'] .'/login.php'

gives me an error:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

The PHP Manual gives this syntax for header() but it gives a similar
error:

header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/login.php");

I gather this header function check will cause my login.php script
re-display which is what I want when the user leaves it blank and
still clicks on the submit button.

Ralph

On Sun, 06 Jul 2003 00:23:21 GMT, "Ian.H [dS]"
<ia*@WINDOZEdigiserv.net> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Sat, 05 Jul 2003 23:55:02 GMT, Ralph
Freshour <ra***@primemail.com> amazingly managed to produce the
following with their Etch-A-Sketch:
I bought the PHP and MySQL For Dummies book and I'm having trouble
understanding how I use PHP to verify and check forms input text
data - the book shows snippets of code so I know how to do the
actual check but I don't understand what web page the php checking
code should go into?

For example, I have a simple login web page (username and password)
and then it calls my mainmenu php web page - I want to make a check
that a username was actually typed in *before* calling the mainmenu
php page - I also know that I can do this check using javascript
but I'd like to stay in php if I can - how can I make the check and
stay on this login form before calling the mainmenu page?

Thanks...


Assuming you have 'username' and 'password' as your form text fields,
and your method is POST:
<?php
if (!preg_match("/([a-zA-Z_.-]+)/", $_POST['username'])) {
@header('Location: http://' .
$_SERVER['HTTP_HOST'] .
'/login.php'
);
}

if (!empty($_POST['password'])) {
// do check for password match
if (md5($_POST['password']) == $required_password) {
@header('Location: http://' .
$_SERVER['HTTP_HOST'] .
'/mainmenu.php'
);
} else {
die('Invalid login');
}
?>
This isn't a coplete solution as you'll need to fill out the
commented areas, but should do what you're after =)
HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPwdr92fqtj251CDhEQJZ2ACfSxH6N4JBiaHnMl8wIJxL+4 Whfy0AoIfT
8wKHusl/p2M0a6HBdp01qcEb
=g6N7
-----END PGP SIGNATURE-----


Jul 16 '05 #3

P: n/a

"Ralph Freshour" <ra***@primemail.com> wrote in message
news:7k********************************@4ax.com...
@header('Location: http://' .$_SERVER['HTTP_HOST'] .'/login.php'

gives me an error:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

The PHP Manual gives this syntax for header() but it gives a similar
error:

header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']
)."/login.php");
I gather this header function check will cause my login.php script
re-display which is what I want when the user leaves it blank and
still clicks on the submit button.

Ralph

On Sun, 06 Jul 2003 00:23:21 GMT, "Ian.H [dS]"
<ia*@WINDOZEdigiserv.net> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Sat, 05 Jul 2003 23:55:02 GMT, Ralph
Freshour <ra***@primemail.com> amazingly managed to produce the
following with their Etch-A-Sketch:
I bought the PHP and MySQL For Dummies book and I'm having trouble
understanding how I use PHP to verify and check forms input text
data - the book shows snippets of code so I know how to do the
actual check but I don't understand what web page the php checking
code should go into?

For example, I have a simple login web page (username and password)
and then it calls my mainmenu php web page - I want to make a check
that a username was actually typed in *before* calling the mainmenu
php page - I also know that I can do this check using javascript
but I'd like to stay in php if I can - how can I make the check and
stay on this login form before calling the mainmenu page?

Thanks...


Assuming you have 'username' and 'password' as your form text fields,
and your method is POST:
<?php
if (!preg_match("/([a-zA-Z_.-]+)/", $_POST['username'])) {
@header('Location: http://' .
$_SERVER['HTTP_HOST'] .
'/login.php'
);
}

if (!empty($_POST['password'])) {
// do check for password match
if (md5($_POST['password']) == $required_password) {
@header('Location: http://' .
$_SERVER['HTTP_HOST'] .
'/mainmenu.php'
);
} else {
die('Invalid login');
}
?>
This isn't a coplete solution as you'll need to fill out the
commented areas, but should do what you're after =)
HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPwdr92fqtj251CDhEQJZ2ACfSxH6N4JBiaHnMl8wIJxL+4 Whfy0AoIfT
8wKHusl/p2M0a6HBdp01qcEb
=g6N7
-----END PGP SIGNATURE-----


Try to use double quote instead of single quote:
$_SERVER["HTTP_HOST"]


Jul 16 '05 #4

P: n/a
What is the code flow with this header() function? When I inserted
your suggested header code, the browser web page didn't do anything -
nothing but the loading bar was moving across like it was trying to do
something or was doing something - is this in an endless loop? I
don't understand how this code is 'triggered' - if it is processed
when the login.php script first loads, then username is always blank
since the user hasn't filled it in yet - maybe another way of asking
this question is when does this if statement with the header code
execute? I have it at the top of my .php script.
On Sun, 06 Jul 2003 03:29:30 GMT, "Steven" <xu*****@comcast.net>
wrote:

"Ralph Freshour" <ra***@primemail.com> wrote in message
news:7k********************************@4ax.com.. .
@header('Location: http://' .$_SERVER['HTTP_HOST'] .'/login.php'

gives me an error:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

The PHP Manual gives this syntax for header() but it gives a similar
error:

header("Location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']
)."/login.php");

I gather this header function check will cause my login.php script
re-display which is what I want when the user leaves it blank and
still clicks on the submit button.

Ralph

On Sun, 06 Jul 2003 00:23:21 GMT, "Ian.H [dS]"
<ia*@WINDOZEdigiserv.net> wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Whilst lounging around on Sat, 05 Jul 2003 23:55:02 GMT, Ralph
>Freshour <ra***@primemail.com> amazingly managed to produce the
>following with their Etch-A-Sketch:
>
>> I bought the PHP and MySQL For Dummies book and I'm having trouble
>> understanding how I use PHP to verify and check forms input text
>> data - the book shows snippets of code so I know how to do the
>> actual check but I don't understand what web page the php checking
>> code should go into?
>>
>> For example, I have a simple login web page (username and password)
>> and then it calls my mainmenu php web page - I want to make a check
>> that a username was actually typed in *before* calling the mainmenu
>> php page - I also know that I can do this check using javascript
>> but I'd like to stay in php if I can - how can I make the check and
>> stay on this login form before calling the mainmenu page?
>>
>> Thanks...
>
>
>
>Assuming you have 'username' and 'password' as your form text fields,
>and your method is POST:
>
>
><?php
> if (!preg_match("/([a-zA-Z_.-]+)/", $_POST['username'])) {
> @header('Location: http://' .
> $_SERVER['HTTP_HOST'] .
> '/login.php'
> );
> }
>
> if (!empty($_POST['password'])) {
> // do check for password match
> if (md5($_POST['password']) == $required_password) {
> @header('Location: http://' .
> $_SERVER['HTTP_HOST'] .
> '/mainmenu.php'
> );
> } else {
> die('Invalid login');
> }
>?>
>
>
>This isn't a coplete solution as you'll need to fill out the
>commented areas, but should do what you're after =)
>
>
>HTH.
>
>
>
>Regards,
>
> Ian
>
>-----BEGIN PGP SIGNATURE-----
>Version: PGP 8.0
>
>iQA/AwUBPwdr92fqtj251CDhEQJZ2ACfSxH6N4JBiaHnMl8wIJxL+4 Whfy0AoIfT
>8wKHusl/p2M0a6HBdp01qcEb
>=g6N7
>-----END PGP SIGNATURE-----


Try to use double quote instead of single quote:
$_SERVER["HTTP_HOST"]


Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.