php - Implementing captcha to my dynamically generated forms -


i have functions generate articles on page + comments associated them:

function comment_form($id) { // generates comment box form every article on page     global $user_data;      if (logged_in() === true) {         echo "         <form method='post' action='' class='comments_form'>             <input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>             <div class='captcha'>" . create_captcha() .  "</div>              <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>             <input type='hidden' name='blog_id' value='$id'>             <input type='submit' name='submit' id='post' value='post'>         </form>         <hr class='artline'>";     } }  function list_articles($rows) {      if (empty($rows)) {         return "there no articles display";     }      $previous_blog_id = 0;      $content = '';      foreach ($rows $row) {         if ($previous_blog_id != $row['content_id']) { // blog id changed             if ($previous_blog_id != 0) { // not first section, close out previous section                 $content .= comment_form($previous_blog_id);              }             // start new blog section             $content .= "<h5 class='posted_by'>posted {$row['posted_by']} on {$row['date']}</h5>                         <h1 class='content_headers'>{$row['title']}</h1>                         <article>{$row['content']}</article>                         <hr class='artline'>";             $previous_blog_id = $row['content_id'];         }         if (!empty($row['comment_by']) && !empty($row['comments'])) {              $content .= "<div class='commented_by'>user: {$row['comment_by']} </div>                    <div class='comments'>comment: {$row['comments']}</div>                    <hr class='artline2'>";         }     }      if ($previous_blog_id != 0) {          $content .= comment_form($previous_blog_id);      }      return $content; }  function insert_comments($comments, $comment_by, $blog_id) {     include('core/db/db_connection.php');      $comment_by = sanitize($comment_by);     $comments = sanitize($comments);     $blog_id = (int)$blog_id;     $sql = "         insert article_comments (                comments,                 comment_by,                 blog_id         )         values (               '$comments',                '$comment_by',                '$blog_id'         )     ";      mysqli_query($dbcon, $sql); } 

i generate simple math captcha per below:

function generate_captcha($num1, $num2) { // generates 2 random numbers     $num1 = (int)$num1;     $num2 = (int)$num2;     $rand_num_1 = mt_rand($num1, $num2);     $rand_num_2 = mt_rand($num1, $num2);     $result = $rand_num_1 + $rand_num_2;      return $result; }   function create_captcha() { // displays captcha on page     $num1 = generate_captcha(1, 20);     $num2 = generate_captcha(1, 20);      echo  $num1 . ' + ' . $num2 . ' = ';     echo '<input type="text" name="captcha_results" size="2">';     echo '<input type="hidden" name=\'num1\' value=' . $num1 . '; ?>';     echo '<input type="hidden" name=\'num2\' value=' . $num2 . '; ?>'; } 

as can see, i'm using create_captcha() function comment_form() function because want every comment box have captcha associated it. same way every article has it's own comment box.

the above code displaying captcha field each comment box have, want. - moves comment boxes above content making this:

|-------------------------------------| // comments form article 1 |name: new user                       | |comment: new comment !               | |                                     |  |-------------------------------------| [submit] [captcha field]  |-------------------------------------| // comments form article 2 |name: new user                       | |comment: new comment !               | |                                     |  |-------------------------------------| [submit] [captcha field]  article_1 title: lorem ipsum content: lorem ipsum dolor sit amet.... -------------------------------------- //comments name: user0 comment: great article! -------------------------------------- name: user1 comment: great article! - 2nd comment  -------------------------------------- // end comments  ============================================================  article_2 title: lorem ipsum content: lorem ipsum dolor sit amet.... -------------------------------------- //comments name: user0 comment: great article! -------------------------------------- name: user1 comment: great article! - 2nd comment  -------------------------------------- // end comments 

the behavior expecting this:

article_1 title: lorem ipsum content: lorem ipsum dolor sit amet.... -------------------------------------- //comments name: user0 comment: great article! -------------------------------------- name: user1 comment: great article! - 2nd comment  -------------------------------------- // end comments |-------------------------------------| // comments form article 1 |name: new user                       | |comment: new comment !               | |                                     |  |-------------------------------------| [submit] [captcha field]  ============================================================  article_2 title: lorem ipsum content: lorem ipsum dolor sit amet.... -------------------------------------- //comments name: user0 comment: great article! -------------------------------------- name: user1 comment: great article! - 2nd comment  -------------------------------------- // end comments |-------------------------------------| // comments form article 2 |name: new user                       | |comment: new comment !               | |                                     |  |-------------------------------------| [submit] [captcha field] 

is position in i'm inserting generate_captcha function causes comment boxes float above content?

edit: if return form instead of echoing - works. comment forms placed below correspondent articles:

function comment_form($id) {     global $user_data;     if (logged_in() === true) {         return <<<eot         <form method='post' action='' class='comments_form'>             <input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>             <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>             <input type='hidden' name='blog_id' value='$id'>             <input type='submit' name='submit' id='post' value='post'>         </form>         <hr class='artline'> eot; 

however, cannot insert function because of <<<eot had use. how can insert create_captcha function above??

edit 2: not return expected captcha form, seems place comment forms in place...

function comment_form($id, $captcha) {      global $user_data;     if (logged_in() === true) {         return <<<eot         <form method='post' action='' class='comments_form'>             <input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>             <textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>             <input type='hidden' name='blog_id' value='$id'>             <input type='submit' name='submit' id='post' value='post'>         </form>         <hr class='artline'> eot;     } }  function list_articles($rows) {     if(empty($rows)){         return "there no articles display";     }      $create_blog_captcha = create_blog_captcha();     $previous_blog_id = 0;      $content = '';      foreach($rows $row) {         if ($previous_blog_id != $row['content_id']) { // blog id changed             if($previous_blog_id != 0) { // not first section, close out previous section                 $content .= comment_form($previous_blog_id, $create_blog_captcha);              }             // start new blog section             $content .= "<h5 class='posted_by'>posted {$row['posted_by']} on {$row['date']}</h5>                         <h1 class='content_headers'>{$row['title']}</h1>                         <article>{$row['content']}</article>                         <hr class='artline'>";             $previous_blog_id = $row['content_id'];         }         if (!empty($row['comment_by']) && !empty($row['comments'])) {              $content .= "<div class='commented_by'>user: {$row['comment_by']} </div>                    <div class='comments'>comment: {$row['comments']}</div>                    <hr class='artline2'>";         }     }      if($previous_blog_id != 0){          $content .= comment_form($previous_blog_id, $create_blog_captcha);      }      return $content; }  function create_blog_captcha() {      $num1 = generate_captcha(1, 20);     $num2 = generate_captcha(1, 20);     $captchanum = $num1 . ' + ' . $num2 . ' = ';     $captchanum .= '<input type="text" name="captcha_results" size="2">                    <input type="hidden" name=\'num1\' value=' . $num1 . '>                    <input type="hidden" name=\'num2\' value=' . $num2 . '>';     return $captchanum; } 

how can return values of create_blog_captcha? i'm doing wrong...

in function create_captcha() have not closed hidden input fields break entire flow of document.

function create_captcha() {     $num1 = generate_captcha(1, 20);     $num2 = generate_captcha(1, 20);      return $num1 . ' + ' . $num2 . ' =      <input type="text" name="captcha_results" size="2">     <input type="hidden" name=\'num1\' value=' . $num1 . ' />     <input type="hidden" name=\'num2\' value=' . $num2 . ' />'; } 

as noted, function echoed content rather return string other function utputting html.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -