PHP Fragen

07/15/2013 22:31 Sorrow91#1
Hallo,

ich habe mir ein Kontaktformular mit Captcha aus dem Internet runtergeladen, hat alles geklappt.

Nun würde ich gerne einen anderen Teil meiner Seite auch mit einem Captcha-Code versehen, das ganze soll ungefähr so aussehen:

Link: [Textfeld]

Captchabild: [Textfeld]

Submit

Hier ist der Code den ich für das Kontaktformular benutze:

HTML Code:
<?php 
$your_email ='[Only registered and activated users can see links. Click Here To Register...]';

session_start();
$errors = '';
$name = '';
$visitor_email = '';
$visitor_skype = '';
$user_message = '';

if(isset($_POST['submit']))
{
	
	$name = $_POST['name'];
	$visitor_email = $_POST['email'];
	$visitor_skype = $_POST['skype'];
	$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)|| empty($visitor_email))
	{
		$errors .= "\n Name and Email are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
	if(empty($_SESSION['6_letters_code'] ) ||
	  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
	//Note: the captcha code is compared case insensitively.
	//if you want case sensitive match, update the check above to
	// strcmp()
		$errors .= "\n The captcha code does not match!";
	}
	
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="Contact (US)";
		$from = $your_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		
		$body = "Name: $name\n".
		"Email: $visitor_email \n".
        "Skype: $visitor_skype \n".
		"Message: \n ".
		"$user_message\n".
		"IP: $ip\n";	
		
		$headers = "From: $from \r\n";
		$headers .= "Reply-To: $visitor_email \r\n";
		
		mail($to, $subject, $body,$headers);
		
		header('Location: refresh.html');
	}
}

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="http://lol-rf-service.tipido.net/favicon.ico" type="image/x-icon" /> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" name="keywords" content="Riot Points, Refer a Friend, Wirb einen Freund, lol, league of legends, refer, referral service"/>
<style type="text/css">
body {
 font-family: Arial;
 font-size: 12px;
 color:#000000;
 text-align: left;
    background-attachment: fixed;
	background-image: url(Pictures/page-bg.jpg);
    background-repeat: no-repeat;
    background-position: left top;
	background-color: #02071b;
}
textarea {
resize: none;
}
a img {
border: none;
}
}
a:link { text-decoration:none; font-weight:bold; color:#CC0000; }
a:visited { text-decoration:none; font-weight:bold; color:#CC0000; }
a img {
border: none;
}
</style>
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
<title>League of Legends (LoL): Referral Service 2.0</title>
</head>
<body>
<table width="700" border="0" cellspacing="0" cellpadding="0" align="center" valign="middle">
  <tr align="center" valign="middle" height="200px">
    <td align="center" valign="middle">
          <img src="Pictures/header_us.png" width="700" height="200" border="0" alt="de" usemap="#de">
  <map name="de">
    <area shape="rect" coords="667,172,690,188" href="../de/kontakt.html" alt="de" title="de">
    </map>
    </td>
    <tr>
    <td background="Pictures/th_grad.png" width="695" height="51px" valign="middle" align="center">
        <a href="index.html"><img src="Pictures/bt_home.png"></a><a href="faq.html"><img src="Pictures/bt_faq.png"></a><a href="vouches.html"><img src="Pictures/bt_vouches.png"></a><a href="status.html"><img src="Pictures/bt_status.png"></a><a href="contact.php"><img src="Pictures/bt_contact.png"></a>    </td>
    </tr>
    <tr height="5px">
    </tr>
  </tr>
   <tr align="center" valign="middle">
<td background="Pictures/content_top.png" height="50px" width="700"></td>
  </tr>
   <tr>
  <td background="Pictures/content.png" height="100%" width="700px">  
<form method="POST" name="contact_form" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> 
<table width="700px" align="left" cellspacing="30px">
<tr>
<td><strong><font size="+2">Contact <br /><br /></font>
</strong>
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id='contact_form_errorloc' class='err'></div>
</td>
</tr>
<tr>
 <td valign="top"> 
  <label for="name">Name</label>
 *</td>
 <td valign="top">
  <input  type="text" name="name" value="<?php echo htmlentities($name) ?>" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address</label>
 *</td>
 <td valign="top">
  <input  type="text" name="email" value="<?php echo htmlentities($visitor_email) ?>" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="skypename">Skypename</label>
 </td>
 <td valign="top">
  <input  type="text" name="skype" value="<?php echo htmlentities($visitor_skype) ?>" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
 </td>
 <td valign="top">
<textarea name="message" rows=8 cols=30
><?php echo htmlentities($user_message) ?></textarea>
 </td>
 </tr>
 <tr>
 <td valign="top">
 <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id="captchaimg" >
<br />
<small>Can't read? Click <a href='javascript: refreshCaptcha();'>here</a> for a new image.</small>
</td>
<td>
<input id="6_letters_code" name="6_letters_code" type="text">
</td>
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit" name="submit">
 </td>
</tr>
</table>
</form>
  </td>
  </tr>
      <td align="center" valign="middle" background="Pictures/content_down.png" height="50px" width="700">
      </td>
      </table>
      <br>
<table align="center" width="700px" height="76" cellpadding="20">
<tr>
<td background="Pictures/footer_2.png" align="center">
<font size="-1" color="#FFFFFF">&copy; LoL-Referral-Service.com</font>
</td>
</tr>
</table>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("contact_form");
//remove the following two lines if you like error message box popups
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();

frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("email","req","Please provide your email"); 
frmvalidator.addValidation("email","email","Email address is not valid"); 
</script>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
	var img = document.images['captchaimg'];
	img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</html>
Ich danke schonmal vorab für eure Hilfe :)
07/16/2013 11:17 telcy#2
Dein Formular sollte ungefähr so aussehen:

PHP Code:

<form name="link_form">

Link: <input type="text" name="link"><br>
Captchabild: <input id="6_letters_code" name="6_letters_code" type="text">
<br/><img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id="captchaimg" >
<small>Can't read? Click <a href='javascript: refreshCaptcha();'>here</a> for a new image.</small>

</form>
Das Formular hat also den Namen "link_form" und wird darüber in deinem Beispiel auch angesprochen.

Also wieder deine Form im Javascript Code aufgreifen und dann sollte schon alles gehen:

PHP Code:
<script language="JavaScript">

var 
frmvalidator  = new Validator("link_form");

frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();

frmvalidator.addValidation("link","req","Please provide your link"); 

</script> 
07/16/2013 12:30 Sorrow91#3
Hey, danke ich probier's später mal aus :)

Quote:
Originally Posted by telcy View Post
Dein Formular sollte ungefähr so aussehen:

PHP Code:

<form name="link_form">

Link: <input type="text" name="link"><br>
Captchabild: <input id="6_letters_code" name="6_letters_code" type="text">
<br/><img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id="captchaimg" >
<small>Can't read? Click <a href='javascript: refreshCaptcha();'>here</a> for a new image.</small>

</form>
Das Formular hat also den Namen "link_form" und wird darüber in deinem Beispiel auch angesprochen.

Also wieder deine Form im Javascript Code aufgreifen und dann sollte schon alles gehen:

PHP Code:
<script language="JavaScript">

var 
frmvalidator  = new Validator("link_form");

frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();

frmvalidator.addValidation("link","req","Please provide your link"); 

</script> 
Klappt soweit super, nun noch eine Frage, wie kann ich es einstellen dass bei dem Feld Link aufjedenfall http:// angegeben werden muss sonst ist dieser unglültig.

Und falls es möglich ist, schon vorgeschrieben ein Text in dem inputfeld "Link" ist, also zb [Only registered and activated users can see links. Click Here To Register...]
Dieser soll dann beim draufklicken verschwinden.

Nochmal vorab Danke :)