Php alert-kind off

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

Post Reply
madness
Forum Newbie
Posts: 9
Joined: Tue Jan 18, 2011 1:54 pm

Php alert-kind off

Post by madness »

Hi guys, im fixing a captcha, but im having some trouble, basically is all ok, i have the page contacts.php and contact_send.php that is where it takes the action.
But i have a problem.

When i put a bad captcher number this goes to the page of contact_send.php and says, "Wrong code", than i have to go back in a browser in do it again, but i want it to make that when i make a bad entry code, it appears like a javascript message saying "wrong code". And redirects me to the incial page to correct that is contacts.php

the code i put it in the contact_send.php is this one.

Code: Select all

if(md5($_POST['code']) != $_SESSION['key']) 
  die("Wrong code!");
Could somedoy help me?

Be well
Nice being a part of the community.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Php alert-kind off

Post by danwguy »

rather than using die("Wrong code"); use...

Code: Select all

if(md5($_POST['code']) != $_SESSION['key']) {
echo "<script language='javascript'>alert('Wrong Code!')</script>";
echo "Please go <a href='javascript:history.go(-1);'><strong>back</strong></a> and use a proper address, then re-submit the form."; //will save all the data that was entered in the form when they go back.
//or use this instead, but use only one of them
echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head><meta http-equiv='REFRESH' content='0;url=http://whereveryouwant.com'>"; //will not save the data the user put in
}else{
//do what you want when the code is correct here
}
madness
Forum Newbie
Posts: 9
Joined: Tue Jan 18, 2011 1:54 pm

Re: Php alert-kind off

Post by madness »

Well, i have to do die, cause this just gonna send me the email even when is incorrect, look:

Code: Select all

<?php 
session_start();
if(md5($_POST['code']) != $_SESSION['key']) {
echo "<script language='javascript'>alert('Wrong code!')</script>";
echo "Por favor vá <a href='javascript&#058;history.go(-1);'><strong>de volta</strong></a> e use blabla."; //keep data

echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head><meta http-equiv='REFRESH' content='0;url=http://domain/contato.php'>"; 
}else{

}

$datahora = "DATA: <B>" . date("d/m/y - H:i:s") . "</B><BR><BR>";

foreach ($_POST as $campo => $valor) {
	if (($campo == 'imageField2_x') or ($campo == 'imageField2_y') or ($campo == 'button') or ($campo == 'subimit_y')) {}else {
		if ($valor <> '') {
			$campo = str_replace("_", " ",$campo);
			$campos .= strtoupper($campo) . ": <b>" . $valor . "</b><Br>";
		}
	}
}

$www = "WWW.domain.com";
$assunto = "CONTACT - domainsite - " . $www;

$conteudo = "CONTACT -domain<br><br>" . $datahora . utf8_decode($campos) . "<br>" . $www;

$para = "myemail";

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
$headers .= "From: domain <general@domain.com>\r\n";
$headers .= "Reply-To: ".$_POST['email']."\r\n";

if ((mail($para,$assunto,$conteudo,$headers)) == true){ ?>
<script>
alert('DAta sent sucess!');
window.location = 'contato.php';
</script>
<? } ?>
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Php alert-kind off

Post by danwguy »

There is no need to use the die("Wrong Code"); if you use the before mentioned code and add all of your code in the else section it will work. Use this...

Code: Select all

<?php 
session_start();
if(md5($_POST['code']) != $_SESSION['key']) {
echo "<script language='javascript'>alert('Wrong code!')</script>";
echo "Por favor vá <a href='javascript&#058;history.go(-1);'><strong>de volta</strong></a> e use blabla."; 
}else{

$datahora = "DATA: <B>" . date("d/m/y - H:i:s") . "</B><BR><BR>";

foreach ($_POST as $campo => $valor) {
        if (($campo == 'imageField2_x') or ($campo == 'imageField2_y') or ($campo == 'button') or ($campo == 'subimit_y')) {}else {
                if ($valor <> '') {
                        $campo = str_replace("_", " ",$campo);
                        $campos .= strtoupper($campo) . ": <b>" . $valor . "</b><Br>";
                }
        }
}

$www = "WWW.domain.com";
$assunto = "CONTACT - domainsite - " . $www;

$conteudo = "CONTACT -domain<br><br>" . $datahora . utf8_decode($campos) . "<br>" . $www;

$para = "myemail";

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
$headers .= "From: domain <general@domain.com>\r\n";
$headers .= "Reply-To: ".$_POST['email']."\r\n";

if ((mail($para,$assunto,$conteudo,$headers)) == true){ ?>
<script>
alert('DAta sent sucess!');
window.location = 'contato.php';
</script>
<? } ?>
 
That will grab the $_POST data, check it with the first if statement. if the first statement "if(md5($_POST['code']) != $_SESSION['key'])" is true it will stop the code and give an alert, it will only send the email if it gets past that first statement. The above code will do exactly what you want, try it yourself.
madness
Forum Newbie
Posts: 9
Joined: Tue Jan 18, 2011 1:54 pm

Re: Php alert-kind off

Post by madness »

Hi, im havind a sintax problem, but i cant see it, its in the last line the "}" but it looks fine

Code: Select all

<?php 
session_start();
if(md5($_POST['code']) != $_SESSION['key']) {
echo "<script language='javascript'>alert('Wrong Code!')</script>";
echo "Please go <a href='javascript&#058;history.go(-1);'><strong>back</strong></a> and use a proper address, then re-submit the form."; //will save all the data that was entered in the form when they go back.
//or use this instead, but use only one of them
echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head><meta http-equiv='REFRESH' content='0;url=http://DOMAIN.COM/contato.php'>"; //will not save the data the user put in
}else{
$datahora = "DATA: <B>" . date("d/m/y - H:i:s") . "</B><BR><BR>";

foreach ($_POST as $campo => $valor) {
	if (($campo == 'imageField2_x') or ($campo == 'imageField2_y') or ($campo == 'button') or ($campo == 'subimit_y')) {}else {
		if ($valor <> '') {
			$campo = str_replace("_", " ",$campo);
			$campos .= strtoupper($campo) . ": <b>" . $valor . "</b><Br>";
		}
	}
}

$www = "WWW.DOMAIN.COM";
$assunto = "CONTACTO - DOMAIN - " . $www;

$conteudo = "CONTACTO - Domain<br><br>" . $datahora . utf8_decode($campos) . "<br>" . $www;

$para = "email";

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
$headers .= "From: Domain <geral@domain.com>\r\n";
$headers .= "Reply-To: ".$_POST['email']."\r\n";

if ((mail($para,$assunto,$conteudo,$headers)) == true){ ?>
<script>
alert('Info send sucess!');
window.location = 'contato.php';
</script>
<? } ?>
}   <------error here

madness
Forum Newbie
Posts: 9
Joined: Tue Jan 18, 2011 1:54 pm

Re: Php alert-kind off

Post by madness »

Thanks a lot, its ok now :D

Just one question, i been having a problem in other sites, i have a captcha but i still receive spam from them, what is the most efective captcha?
;)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Php alert-kind off

Post by Jonah Bron »

reCAPTCHA is pretty popular.

http://www.google.com/recaptcha
Post Reply