-
Hallo,
Wie der Titel schon sagt, habe ich ein Problem mit dem einbinden des Captchas in die "comment.class.php"
comment.class.php:
und hiermit überprüfe ich die Captchaeingabe:PHP-Code:<?php
class Comment
{
private $data = array();
public function __construct($row)
{
/*
/ The constructor
*/
$this->data = $row;
}
public function markup()
{
/*
/ This method outputs the XHTML markup of the comment
*/
// Setting up an alias, so we don't have to write $this->data every time:
$d = &$this->data;
$link_open = '';
$link_close = '';
if($d['url']){
// If the person has entered a URL when adding a comment,
// define opening and closing hyperlink tags
$link_open = '<a href="'.$d['url'].'">';
$link_close = '</a>';
}
// Converting the time to a UNIX timestamp:
$d['dt'] = strtotime($d['dt']);
// Needed for the default gravatar image:
$url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';
return '
<div class="comment">
<div class="avatar">
'.$link_open.'
<img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&default='.urlencode($url).'" />
'.$link_close.'
</div>
<div class="name">'.$link_open.$d['name'].$link_close.'</div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
<p>'.$d['body'].'</p>
</div>
';
}
public static function validate(&$arr)
{
/*
/ This method is used to validate the data sent via AJAX.
/
/ It return true/false depending on whether the data is valid, and populates
/ the $arr array passed as a paremter (notice the ampersand above) with
/ either the valid input data, or the error messages.
*/
$errors = array();
$data = array();
// Using the filter_input function introduced in PHP 5.2.0
if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
{
$errors['email'] = 'Please enter a valid Email.';
}
if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL)))
{
// If the URL field was not populated with a valid URL,
// act as if no URL was entered at all:
$url = '';
}
// Using the filter with a custom callback function:
if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
{
$errors['body'] = 'Please enter a comment body.';
}
if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
{
$errors['name'] = 'Please enter a name.';
}
if(!empty($errors)){
// If there are errors, copy the $errors array to $arr:
$arr = $errors;
return false;
}
// If the data is valid, sanitize all the data and copy it to $arr:
foreach($data as $k=>$v){
$arr[$k] = mysql_real_escape_string($v);
}
// Ensure that the email is lower case:
$arr['email'] = strtolower(trim($arr['email']));
return true;
}
private static function validate_text($str)
{
/*
/ This method is used internally as a FILTER_CALLBACK
*/
if(mb_strlen($str,'utf8')<1)
return false;
// Encode all html special characters (<, >, ", & .. etc) and convert
// the new line characters to <br> tags:
$str = nl2br(htmlspecialchars($str));
// Remove the new line characters that are left
$str = str_replace(array(chr(10),chr(13)),'',$str);
return $str;
}
}
?>
Als leztes wird dann , wenn alles richtig war, mit:PHP-Code:if ( md5($income['captcha']) == $_SESSION['captcha'] )
{
// Skript...
}
submit.php
in die MySQL DB gespeichert. Daher dachte ich, dass ich die Captcha überprüfung bei der "comment.class.php" erledige.PHP-Code:<?php
// Error reporting:
error_reporting(E_ALL^E_NOTICE);
include "connect.php";
include "comment.class.php";
/*
/ This array is going to be populated with either
/ the data that was sent to the script, or the
/ error messages.
/*/
$arr = array();
$validates = Comment::validate($arr);
if($validates)
{
/* Everything is OK, insert to database: */
mysql_query(" INSERT INTO comments(name,url,email,body,ip)
VALUES (
'".$arr['name']."',
'".$arr['url']."',
'".$arr['email']."',
'".$arr['body']."',
'".$_SERVER['REMOTE_ADDR']."'
)");
$arr['dt'] = date('r',time());
$arr['id'] = mysql_insert_id();
/*
/ The data in $arr is escaped for the mysql query,
/ but we need the unescaped variables, so we apply,
/ stripslashes to all the elements in the array:
/*/
$arr = array_map('stripslashes',$arr);
$insertedComment = new Comment($arr);
/* Outputting the markup of the just-inserted comment: */
echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));
}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}
?>
Wer eine Idee hat bitte melden
Anhang noch "script.php":
PHP-Code:$(document).ready(function(){
/* The following code is executed once the DOM is loaded */
/* This flag will prevent multiple comment submits: */
var working = false;
/* Listening for the submit event of the form: */
$('#addCommentForm').submit(function(e){
e.preventDefault();
if(working) return false;
working = true;
$('#submit').val('Working..');
$('span.error').remove();
/* Sending the form fileds to submit.php: */
$.post('submit.php',$(this).serialize(),function(msg){
working = false;
$('#submit').val('Submit');
if(msg.status){
/*
/ If the insert was successful, add the comment
/ below the last one on the page with a slideDown effect
/*/
$(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
$('#body').val('');
}
else {
/*
/ If there were errors, loop through the
/ msg.errors object and display them on the page
/*/
$.each(msg.errors,function(k,v){
$('label[for='+k+']').append('<span class="error">'+v+'</span>');
});
}
},'json');
});
});
Achja wundert euch nicht ist jQuery + MySQL + PHP zusammen darum manchmal so "kompliziert".
Wenn ihr noch fragen habt oder welche fehler gesehen habt, bitte melden!
Mein erster Post Mein erster Thread hiermit möchte ich mich noch vorstellen
.
PS: Ich erwarte nicht, dass ihr hellsehen könnt also müsst/könnt ihr mir villeicht nur Tipps geben danke
.
Gruss 0grish
EDIT: hier die Quelle alle codes:
http://tutorialzine.com/2010/06/simp...enting-system/Geändert von ogrish (03.07.10 um 18:04 Uhr) Grund: Link hinzugefügt
-
04.07.10 09:22 #2
- Registriert seit
- May 2006
- Ort
- There is no place like 127.0.0.1
- Beiträge
- 3.521
Moin,
und was ist deine Frage? Ich meine was funktioniert denn nicht?Grüße
--
Qualität des Codes wird in WTF's/Min gemessen: Je mehr, desto schlechter der Code ;-)
-
04.07.10 10:24 #3mfg ComFreek
Falls ich dir geholfen habe, würde ich mich über ein DANKE freuen!
Kenn mich am besten aus in C++, WEB-Sprachen (PHP, HTML, JavaScript) und vllt. mehr
[PHP] Überprüfen, ob Website erreichbar • Sicherheit in PHP-Codes schaffen • Google Chrome-Extension für tutorials.de • json_compress()
-
Bei Problemen mit Codes, postet bitte den entsprechenden Codeausschnitt und setzt den in entsprechende Tags.
( [cpp] [/cpp] [css] [/css] [html] [/html] [java] [/java] [javascript] [/javascript] [php] [/php] [sql] [/sql] )
"Funktioniert nicht" ist keine Fehlermeldung. Bitte eine genaue Fehlerbeschreibung und, wenn vorhanden, Fehlermeldungen posten.
RegEx Tutorial
PHP Funktionsreferenz
-
04.07.10 11:09 #5Maik Tutorials.de Gastzugang
Hi.Das ist von Fall zu Fall unterschiedlich, eine allgemeingültige / pauschale Aussage gibt's daher nicht.
Um Gewißheit zu haben, was zulässig ist, und was hierbei zu beachten ist, wirft man ganz einfach einen Blick in die Lizenzbestimmungen des Anbieters.
Den erforderlichen "Back-Link" zum Artikel hat ogrish in seinem Beitrag gesetzt, also spricht auch nichts dagegen, den Quellcode hier zu posten.
Zitat von [url=http://tutorialzine.com/license/]http://tutorialzine.com/license/[/url]
Glauben heißt nicht wissen.
Wie gesagt, ein kurzer Blick in die Lizenzbestimmungen klärt die Frage eindeutig, anstatt hier weitere Mutmaßungen zu streuen.
mfg Maik
-
04.07.10 11:25 #6
Irgendwie hätte ich mir denken können, dass man unter Lizenzbestiimungen gucken muss. Aber danke für die ausführliche Erklärung!
mfg ComFreek
Falls ich dir geholfen habe, würde ich mich über ein DANKE freuen!
Kenn mich am besten aus in C++, WEB-Sprachen (PHP, HTML, JavaScript) und vllt. mehr
[PHP] Überprüfen, ob Website erreichbar • Sicherheit in PHP-Codes schaffen • Google Chrome-Extension für tutorials.de • json_compress()
-
Kurz und einfach: Ich möchte einen "Sicherheitscode" einbinden und die eingabe überprüfen.
Ich habe es schon so versucht:
bei submit.php:
, aber das erkennt sogar ein blinder ohne Stock, dass das nicht stimmen kann.PHP-Code:/* ................. */
if ( md5($income['captcha']) == $_SESSION['captcha'] )
{
elseif($validates)
{
/* Everything is OK, insert to database: */
/*......................*/
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}
}
Dann noch die Form:
Gruss ogrishHTML-Code:<div id="addCommentContainer"> <p>Add a Comment</p> <form id="addCommentForm" method="post" action=""> <div> <label for="name">Your Name</label> <input type="text" name="name" id="name" /> <label for="email">Your Email</label> <input type="text" name="email" id="email" /> <label for="url">Website (not required)</label> <input type="text" name="url" id="url" /> <label for="body">Comment Body</label> <textarea name="body" id="body" cols="20" rows="5"></textarea> <label for="captcha">Captchacode</label><br> <img src="captcha/captcha.php" border="0" alt="captcha"><br> <input name="captcha" size="5" type="text"> <input type="submit" id="submit" value="Submit" /> </div> </form> </div>
-
04.07.10 12:12 #8
Du hast auch einen Fehler bei den IF-Struktueren:
Irgendwie hast du kein IF zu ELSEIF und eine schließende Klammer fehlt!PHP-Code:/* ................. */
if ( md5($income['captcha']) == $_SESSION['captcha'] )
{
elseif($validates)
{
/* Everything is OK, insert to database: */
/*......................*/
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}
}
mfg ComFreek
Falls ich dir geholfen habe, würde ich mich über ein DANKE freuen!
Kenn mich am besten aus in C++, WEB-Sprachen (PHP, HTML, JavaScript) und vllt. mehr
[PHP] Überprüfen, ob Website erreichbar • Sicherheit in PHP-Codes schaffen • Google Chrome-Extension für tutorials.de • json_compress()
-
Sorry hier der ganze code ab IF:
Ich schau es mir kurz an.PHP-Code:if ( md5($income['captcha']) == $_SESSION['captcha'] )
{
elseif($validates)
{
/* Everything is OK, insert to database: */
mysql_query(" INSERT INTO comments(name,url,email,body,ip)
VALUES (
'".$arr['name']."',
'".$arr['url']."',
'".$arr['email']."',
'".$arr['body']."',
'".$_SERVER['REMOTE_ADDR']."'
)");
$arr['dt'] = date('r',time());
$arr['id'] = mysql_insert_id();
/*
/ The data in $arr is escaped for the mysql query,
/ but we need the unescaped variables, so we apply,
/ stripslashes to all the elements in the array:
/*/
$arr = array_map('stripslashes',$arr);
$insertedComment = new Comment($arr);
/* Outputting the markup of the just-inserted comment: */
echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));
}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}
}
-
06.07.10 08:43 #10
- Registriert seit
- Nov 2007
- Beiträge
- 94
Alternativ kannst du auch diese doppelte IF abfrage durch eine AND verknüpfung lösen, also:
PHP-Code:if ( md5($income['captcha']) == $_SESSION['captcha'] && $validates == true) {
// Code...
}
else {
//anderer Code bzgl des errors
}
Geändert von NoUse4aNick (06.07.10 um 08:46 Uhr)
-
OK danke
.
Da ich später coden möchte in php und das auch können möchte / WILL, kaufe ich mir ein PHP5 & MySQL5 buch
.
PS: NoUse4aNick ich werde es versuchen danke viel mal!
EDIT:
Es hat nicht geklappt mit:
PHP-Code:if ( md5($income['captcha']) == $_SESSION['captcha'] && $validates == true) {
// Code...
}
else {
//anderer Code bzgl des errors
}
Habe das beim index:
Alles wird angezeigt, jedoch steht dan beim button nur: "Working..."HTML-Code:<label for="captcha">Captchacode</label><br> <img src="captcha/captcha.php" border="0" alt="captcha"><br> <input name="captcha" size="5" type="text" id="captcha">
und bei der MySQL Datenbank wurde auch nichts eingetragen?
Gruss 0grishGeändert von ogrish (06.07.10 um 17:35 Uhr)
-
07.07.10 10:59 #12
- Registriert seit
- Nov 2007
- Beiträge
- 94
Gib dir doch mal kurz zum debuggen die Werte der Variablen vor deiner If abfrage aus:
Mit diesen werten solltest du eigenltich den Fehler analysieren können. Ansonsten schreib doch mal deinen output hier hienein.PHP-Code:echo md5($income['captcha'])."<br />". $_SESSION['captcha']."<br />".$validates."<br />";
lg NoUse4aNick
-
der md5 kommt beim debuggen : d41d8cd98f00b204e9800998ecf8427e
sollte ja alles klappen
EDIT:
Habe ein neuen Captcha reingemacht jedoch zeigt es mir nun in ROT wo sonst die ERRORS angezeigt werden, denn Inhalt der Textfelder...
Hier beim submit.php:
PHP-Code:<?php
// Error reporting:
error_reporting(E_ALL^E_NOTICE);
include "connect.php";
include "comment.class.php";
/*
/ This array is going to be populated with either
/ the data that was sent to the script, or the
/ error messages.
/*/
$arr = array();
$validates = Comment::validate($arr);
/* Hier der neue Code: */
if((isset($_SESSION['captcha_spam']) AND $_POST["sicherheitscode"] == $_SESSION['captcha_spam']) && $validates == true){
unset($_SESSION['captcha_spam']);
/* Hier der neue Code ENDE */
/* Everything is OK, insert to database: */
mysql_query(" INSERT INTO comments(name,url,email,body)
VALUES (
'".$arr['name']."',
'".$arr['url']."',
'".$arr['email']."',
'".$arr['body']."'
)");
$arr['dt'] = date('r',time());
$arr['id'] = mysql_insert_id();
/*
/ The data in $arr is escaped for the mysql query,
/ but we need the unescaped variables, so we apply,
/ stripslashes to all the elements in the array:
/*/
$arr = array_map('stripslashes',$arr);
$insertedComment = new Comment($arr);
/* Outputting the markup of the just-inserted comment: */
echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));
}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}
?>Geändert von ogrish (07.07.10 um 19:16 Uhr) Grund: Neuer Captcha code
-
07.07.10 19:14 #14
- Registriert seit
- Mar 2008
- Ort
- Hermagor (Kärnten)
- Beiträge
- 25
was is ein captcha?
mfg
-
07.07.10 19:17 #15
Guck doch einfach bei Wikipedia oder bei Google!!
mfg ComFreek
Falls ich dir geholfen habe, würde ich mich über ein DANKE freuen!
Kenn mich am besten aus in C++, WEB-Sprachen (PHP, HTML, JavaScript) und vllt. mehr
[PHP] Überprüfen, ob Website erreichbar • Sicherheit in PHP-Codes schaffen • Google Chrome-Extension für tutorials.de • json_compress()
Ähnliche Themen
-
Formular mit Javascript php captcha einbinden
Von zyclop im Forum PHPAntworten: 37Letzter Beitrag: 13.01.10, 13:09 -
Eclipse-Debugger: Class File Editor findet ResourceBundle.class nicht
Von snoopysalive im Forum JavaAntworten: 2Letzter Beitrag: 17.03.08, 16:41 -
Captcha Problem (einbinden)
Von Benard im Forum PHPAntworten: 6Letzter Beitrag: 22.05.06, 12:41 -
java class datei in jsp einbinden
Von TheNewbie im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 0Letzter Beitrag: 05.06.04, 09:43 -
TextIO.class für vereinfachte ein/ausgabe einbinden
Von Raphael im Forum JavaAntworten: 1Letzter Beitrag: 14.11.03, 01:41



1Danke

Zitieren



Login






[PHP][Snippet] Array zu XML konvertieren