AdRock Posted August 13, 2006 Share Posted August 13, 2006 I have tried a few example on checking an email address format but I can't get any to work unless I am doing it wrong.I tried a couple of examples and some of them reported errors so am looking for adviceHere is my code and i've taken out the code that causes the errors[code]<?session_start();session_register("session");if(!isset($session['userid'])){echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>";exit;}include 'includes/connection.php';$query = "SELECT * FROM users WHERE userid = '".$_SESSION['userid']."'";$result = mysql_query($query);while($row = mysql_fetch_assoc($result)){$id = $row['userid'];$firstname = $row['first_name'];$lastname = $row['last_name'];$emailaddress = $row['email_address'];$db_password = $row['password'];}// This is displayed if all the fields are not filled in$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";// Convert to simple variables $first_name = stripslashes($_POST['first_name']); $last_name = stripslashes($_POST['last_name']);$email_address = stripslashes($_POST['email_address']);if (!isset($_POST['first_name'])) {?><h2>Update personal details!</h2><form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <p class="style3"><label for="first_name" style="width:8em">First Name:</label> <input type="text" title="Please enter your first name" name="first_name" size="30" value="<? echo $firstname; ?>"/></p> <p class="style3"><label for=last_name" style="width:8em">Second Name:</label> <input type="text" title="Please enter your last name" name="last_name" size="30" value="<? echo $lastname; ?>"/></p> <p class="style3"><label for="email_address" style="width:8em">Email address:</label> <input type="text" title="Enter your email address" name="email_address" size="30" value="<? echo $emailaddress; ?>"/></p> <p class="style3"><label title="Login"> <input type="submit" value="Update" style="margin-left:97px" class="submit-button"/></label></p></form><?php}elseif (empty($first_name) || empty($last_name) || empty($email_address)) { echo $empty_fields_message;}else {if (strcmp( $first_name,$firstname ) !=0 || strcmp( $last_name,$lastname ) !=0 || strcmp( $email_address,$emailaddress ) !=0){ if(mysql_query("update users SET first_name='$first_name', last_name='$last_name', email_address='$email_address' WHERE userid='$id'")){ if (strcmp( $email_address,$emailaddress ) !=0) {[/code]I want to put the validation after the end of this code.In one exapmple i looked at from http://www.spoono.com/php/tutorials/tutorial.php?id=41 i got some errors about fsocks.I am looking for a tutorial or example that does actually work and check for email text box $email_address Quote Link to comment Share on other sites More sharing options...
whare Posted August 13, 2006 Share Posted August 13, 2006 I not sure if this is what you want but i hope it is its the members system by phpfreak using email verification for account activationhttp://www.phpfreaks.com/tutorials/40/0.phpHope it helps anyway Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 13, 2006 Author Share Posted August 13, 2006 I have used this for my members system but i need to prevent a user entering an invalid email address.Currently there is nothing stopping the user entering "lgkhgsdhgjsfdhi" as their email address. I want something that looks for valid characters such as . and @ Quote Link to comment Share on other sites More sharing options...
ScottRiley Posted August 13, 2006 Share Posted August 13, 2006 strchr(string,search)string refers to teh string you'll be searching, in this case the e-mail address and the character you want to search for.strchr($email, "@"); would search $email for @, if @ is nto in email, teh result will be false[code]<?phpif (strchr($email, "@") and strchr($email, ".")) //contains "@" and "."{ email is valid}else{ email is invalid}?>[/code] Quote Link to comment Share on other sites More sharing options...
Orio Posted August 13, 2006 Share Posted August 13, 2006 @ScottRiley- Is the following string a valid email- "@$#^^.."?Here is what I use:[code]<?phpif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})%body%quot;, $email)){echo "email invalid!";}else{echo "email valid!";}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
ScottRiley Posted August 13, 2006 Share Posted August 13, 2006 Thanks for pointing that out, I'm still a n00b when it comes to PHP ::), and I haven't came across the 'eregi' statement yet, i checked the manual and it seems very useful, thanks. Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 13, 2006 Author Share Posted August 13, 2006 I got some code from [url=http://www.spoono.com/php/tutorials/tutorial.php?id=41]http://www.spoono.com/php/tutorials/tutorial.php?id=41[/url] and now I get this error.Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/adrock/public_html/jack/email.php on line 17Warning: fsockopen(): unable to connect to :25 in /home/adrock/public_html/jack/email.php on line 17E-mail entered is not valid.I just wanted to make sure the code worked so i knocked up a quick form[code]<?function checkEmail($email) { if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) { return FALSE; } list($Username, $Domain) = split("@",$email); if(getmxrr($Domain, $MXHost)) { return TRUE; } else { if(fsockopen($Domain, 25, $errno, $errstr, 30)) { return TRUE; } else { return FALSE; } }}$email = $_POST['email_address'];{?><form method="post" action="<? $_SERVER['PHP_SELF']; ?>">Email<input type="text" name="email_address" size="30"/><input type="submit" value="Register"></form><?}if(checkEmail($email) == FALSE) { echo "E-mail entered is not valid.";} else { echo "E-mail entered is valid.";}?>[/code]I don't know if it's something i did wrong Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 13, 2006 Share Posted August 13, 2006 Try:[code]<?phpfunction checkEmail($email) { if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) { return FALSE; } list($Username, $Domain) = split("@",$email); if(getmxrr($Domain, $MXHost)) { return TRUE; } else { if(fsockopen($Domain, 25, $errno, $errstr, 30)) { return TRUE; } else { return FALSE; } }}if (isset($_POST['submit'])) { $email = trim(strip_tags($_POST['email_address'])); if(checkEmail($email) == FALSE) { echo "E-mail entered is not valid."; } else { echo "E-mail entered is valid."; }} else {?><form method="post" action="<? $_SERVER['PHP_SELF']; ?>">Email<input type="text" name="email_address" size="30"/><input type="submit" name="submit" value="Register"></form><?php}?>[/code]Modified, tested, works for me. Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 13, 2006 Author Share Posted August 13, 2006 I tried that and i get this error nowParse error: syntax error, unexpected '{' in /home/adrock/public_html/jack/email.php on line 27I can't see why it's reporting that error Quote Link to comment Share on other sites More sharing options...
toplancers Posted August 13, 2006 Share Posted August 13, 2006 $regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$'; if (eregi($regex, $email)) { $key = 1; } else { echo("Please enter a valid Email address!"); }http://TopLancers.comProfessionals and Freelancers create your profile today!http://TopLancers.com/forumJoin today! Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 13, 2006 Share Posted August 13, 2006 [quote author=AdRock link=topic=104080.msg415152#msg415152 date=1155508125]I tried that and i get this error now ...[/quote]100% my fault, I missed a closing ) in checking the $_POST['submit']. My original code has been modified to correct it. Quote Link to comment Share on other sites More sharing options...
AdRock Posted August 14, 2006 Author Share Posted August 14, 2006 Thanks.....that works perfectlyBTW...how long does it take to check an invalid email. To check a valid one takes a split second but if i put a radom domain in it takes ages. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 14, 2006 Share Posted August 14, 2006 Unlike life, success is quicker than failure ;DI'm sure there's a technical reason though. Maybe it's because the valid domains you test with are all in the same zone as you whereas a random domain could exist anywhere on the planet ... or not at all, in which case there's possibly retry times involved with the socket access. Perhaps someone who really knows can explain it to you.A potential downside to checking the domain could be that it is completely valid but 'temporarily' inaccessible (nothing has 100% uptime) which would cause your script to think it didn't exist when it really did. Also, don't forget that just because the domain name exists it doesn't mean the email address exists. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.