Jump to content


Photo

Help Required

help

  • Please log in to reply
5 replies to this topic

#1 Scoopsowl

Scoopsowl

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 31 December 2012 - 10:42 AM

Hi,
I am new to php programming but found a script that checks email validation. The script works fine but instead of displaying the error message "Email address is invalid" I would like the script to go to an error page that I have designed. Below is a section of the script I am using. Any ideas?

stristr($_REQUESTemail," ")){$errors[] = "Email address is invalid";}else{$

Thanks

#2 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 31 December 2012 - 11:10 AM

You can redirect from there using the header("Location: ...") function but without seeing the rest of the script no-one can tell you if its a good idea or not as additional logic may be carried out after that if statement.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#3 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 31 December 2012 - 11:25 AM

Don't forget to call die() after the header.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#4 Scoopsowl

Scoopsowl

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 31 December 2012 - 11:40 AM

Thanks for the quick replies. Please find below the expanded code.
Once again..many thanks

// Validate email field.

if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
{

$_REQUEST['email'] = trim($_REQUEST['email']);

if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

}

#5 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 31 December 2012 - 02:37 PM

First off: Please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read. Thank you.

Secondly: You shouldn't redirect the user upon a validation error, which an invalid/missing e-mail address is. What you should do is repopulate the form with the input from the user, add an error message telling what's wrong, and then show the form anew.
Only after the input has been validated, and processed properly (in this case, e-mailed) should you redirect the user. Doing so to prevent a refresh of the page to re-submit the data, and thus preventing an erroneous duplication of said data.
Keeping it simple.

#6 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 31 December 2012 - 02:47 PM

That code wouldn't be readable with code tags anyway.
OP: Click the toggle button in the top left of the text editor to get to plain text and copy and paste your code in there to preserve the formatting.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com