validate user input

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

validate user input

Post by fresh »

hey,

I'm just trying to code a quick and simple script which will validate a text box, it will be looking for the correct pass, without using SQL... I want to embed the pass within the PHP script and validate it from the PHP script...

My php script: cha1.php

Code: Select all

<?php
if ($pass == "AbraCadabra") ; &#123;
echo = "Correct!" ;
else &#123;
echo = "Wrong!" ;
&#125;&#125;
?>
My Htm script: tester.htm

Code: Select all

<html>
<head>
	<title></title>
</head>

<body>

<form action="/cha1.php">
<input type="text" name="pass">
<input type="submit" value="Check Pass">
</form>

</body>
</html>
My php script is the problem obviously, I just posted it to give you an idea of what I'm trying to do here, can someone please reffer me to a page, or help tweak my code to work...either way I'd be appreciative...thanks
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

I'm just guessing because you didn't include the error message.

Code: Select all

<?php 
if ($pass == "AbraCadabra") &#123; 
  echo "Correct!"; 
&#125; else &#123; 
  echo "Wrong!"; 
&#125; 
?>
Take a look at this one where the PHP and HTML are combined:
http://www.totallyphp.co.uk/scripts/pas ... a_page.htm
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Your php code needs some help. First, your braces are a little crazy, and you have a semi-colon after your IF statement.
IF's should be written as such:

Code: Select all

<?php
if ($a == "hello") {
    echo "hello!";
} else {
    echo "goodbye!";
}
?>
(Although I prefer the following formatting, which is technically 'incorrect')

Code: Select all

<?php
if ($a == "hello")
{
    echo "Hello!";
}
else
{
    echo "Goodbye!";
}
?>
Both bits of code do the same thing, but are laid out differently.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

alright :)

Post by fresh »

thank you guys so much... I preffer the second format too, I code like that in jscript, easier to read right? anyway, thanks alot, you've been a big help...wasn't sure if I could use the if & else statement the way I was, you cleared it up, again thanks alot...
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

oh man ... lol

Post by fresh »

ok it's defaulting everything to wrong even when the right pass is inputted and submitted...

Code: Select all

<?php
if ($pass == "Ave Maria") &#123;
  echo "Correct!";
&#125; else &#123;
  echo "Wrong!";
&#125;
?>
thats the code I am using... that's correct right? again the htm page is:

Code: Select all

<html>
<head>
   <title></title>
</head>

<body>

<form action="/cha1.php">
<input type="text" name="pass">
<input type="submit" value="Check Pass">
</form>

</body>
</html>
there isn't a problem with my htm script is there...somethings not working right for some reason, I dont get it, seems logical....

anyway, here's the page I'm testing it on:

http://newb.t35.com/tester.htm

don't worry about malicious code or anything like that, I'm not like that... so if you want to see for yourself what's happening, then please feel free to visit the page... oh yeah, the server adds some popup script to my pages automatically, so turn on your blockers before entering the page, I can't do anything about it, so other than that you should be cool ... thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if ($_GET['pass'] == "Ave Maria") {

you don't have register globals on.. (which is correct)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Whoa! I'd say it may be time to find a new server!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

magicrobotmonkey wrote:Whoa! I'd say it may be time to find a new server!
agreed, hosts that add anything to a page automatically, without our asking it isn't cool.. even if free.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

i know, i know...

Post by fresh »

i thought I outta warn you guys...it got me once and was nothing but a headache, got caught in one of their loops and it sucked... anyway, I'll try to add that piece into the script I have now and see what that does for me...again, like always your help is appreciated very much!! Good day.


edit: Woo hoo, that did it... now I can take a break, lol...thank you
Last edited by fresh on Mon Jun 14, 2004 12:27 pm, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Try adding an echo statement to the end of your php so you can see what it thinks $pass is, ie:

Code: Select all

<?php 
if ($pass == "Ave Maria") { 
  echo "Correct!"; 
} else { 
  echo "Wrong!"; 
}
echo "<br><br>Pass = $pass"; 
?>
This sort of thing is essential for debugging, just remove it before you go live!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

this should probably be your code bro :

Code: Select all

<?php 
$pass = $_POST['pass'];
if ($pass == "Ave Maria")
{ 
  echo "Correct!"; 
}
else
{ 
  echo "Wrong!"; 
} 
?>
the htm page :

Code: Select all

<html> 
<head> 
   <title></title> 
</head> 

<body> 

<form action="cha1.php" method="post"> 
<input type="password" name="pass"> 
<input type="submit" value="Check Pass"> 
</form> 

</body> 
</html>
Last edited by infolock on Mon Jun 14, 2004 12:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isn't the default method GET in a form?
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

cool

Post by fresh »

hey that's a good tip, thanks! Kinda like an alert box for javascript, not really, but serves the same purpose...cool... oh and for my own clarifications if I was to want to have multiple answers, I would need to declare a global var right? So I would just go like this right:

$var = "Bill";
$Var = "Jane";

I assume that's what I wold do... then I'd just call that variable in my if statement to compare it to user input... anyway, I'll read up on it... thanks
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

feyd : yup, thought he had already put method="post" in it :oops:
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

well

Post by fresh »

im too new to say but I think both would do the trick in this case atleast... that's something to research later on... alright I got get back to work, lol, thanks alot guys, you made my day... later
Post Reply