Page 1 of 1

validating email

Posted: Thu Nov 27, 2003 5:53 am
by charmedp3
hi where should i add this script to

Code: Select all

<? 
function validate_email ($address) { 
   return (ereg('^[-!#$%&''*+\\./0-9=?A-Z^_`a-z{|}~]+'. 
                '@'. 
                '[-!#$%&''*+\\/0-9=?A-Z^_`a-z{|}~]+\.'. 
                '[-!#$%&''*+\\./0-9=?A-Z^_`a-z{|}~]+$', 
                $address), 1); 
} 

?>
==============================================
this is my register.php

Code: Select all

<?php
$dbserver="127.0.0.1";		// Server to connect to, leave this unchanged
$dbuser="charmed_bhs";	// Username, change this to whatever you need
$dbpass="123";		// Same for pass
$db="charmed_bhs";		// Database name, u need to change this


// End settings


if($_POST['check']=="Register"){
mysql_connect($dbserver,$dbuser,$dbpass);
mysql_select_db($db);
$_SESSION['myuser']=$username;$_SESSION['mypass']=$password;$_SESSION['myemail']=$email;$_SESSION['myname']=$myname;$_SESSION['myage']=$age;$_SESSION['myhobbies']=$hobbies;
if (!$username) {echo "<font face=verdana size=2>You did not provide a username!<br></font>"; $signup="no";}
if (!$password) {echo "<font face=verdana size=2>You did not provide a password!<br></font>"; $signup="no";}
if (!$email) {echo "<font face=verdana size=2>You did not provide a email address!<br></font>"; $signup="no";}
if (!$name) {echo "<font face=verdana size=2>You did not enter your name!<br></font>"; $signup="no";}
if (!$age) {echo "<font face=verdana size=2>You did not enter your age!<br></font>"; $signup="no";}
if (!$hobbies) {echo "<font face=verdana size=2>You did not enter your hobbies!</font>"; $signup="no";}
if ($signup=="no"){echo "<font face=verdana size=2><br><br><--<a href=register.php>Go</a> back and try again</font>";}
else {
$query="SELECT * FROM users WHERE username='$username'";
$result=mysql_query($query); $num_rows = mysql_num_rows($result);
if ($num_rows!="0"){mysql_close(); echo "<br><center><font face=verdana size=2>User exists.<br><br>Please go [<a href=./register.php>back</a>] and try again</font></center>";}
else {
mysql_query("INSERT INTO users (Username,Password,Email,Name,Age,Sex,Hobbies) VALUES ( '$username','$password','$email','$name','$age','$sex','$hobbies')"); mysql_close();
echo "<br><br><center><font face=verdana size=2>Username '$username' has been successfully added to our member database.<br>Thank you for verifying as a member of BHS<br></font><br>";}}}
// Form code
else{
echo "<form action=register.php method=post><font face=verdana size=2>Username : </font><input type=text name=username value=$myuser><font face=verdana size=2><br>Password : </font><input type=password name=password value=$mypass><font face=verdana size=2><br>Email : </font><input type=text name=email value=$myemail><font face=verdana size=2><br>Name : </font><input type=text name=name value=$myname><font face=verdana size=2><br>Age : </font><input type=text name=age value=$myage><font face=verdana size=2><br>Sex : </font><select name=sex><option>Male</option><option>Female</option></select><font face=verdana size=2><br>Hobbies : </font><input type=text name=hobbies value=$myhobbies><br><br><input type=submit name=check value=Register><input type=reset value=Reset name=reset></form>";}


?>
<?php include "end.php"; ?>

Posted: Thu Nov 27, 2003 9:21 am
by twigletmac
You need to add the call to the function at the point where you validate the $email address. The function itself could be anywhere in the page it doesn't matter.

You'll probably find it a lot easier to work with your code if you only have one statement per line and add a bit of indenting.

Mac

Posted: Thu Nov 27, 2003 10:35 am
by m3mn0n
Functions need to be defined at the very top of the script, before headers and other variable definitions.

Posted: Thu Nov 27, 2003 8:16 pm
by charmedp3
can u help me code that part..i am extremely new to php.

Posted: Fri Nov 28, 2003 3:09 am
by twigletmac
Sami wrote:Functions need to be defined at the very top of the script, before headers and other variable definitions.
Nope, they can go at the bottom and be called at the top if you like.

http://php.net/manual/en/functions.php# ... er-defined
PHP manual wrote: In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4. Except when a function is conditionally defined...
Mac