php会话变量只提供旧的会话值

时间:2014-03-21 09:15:56

标签: javascript session-variables captcha php

我正在使用wordpress网站,在其中我有一个页面,它使用会话来存储验证码并在另一个页面(联系页面)中检索它,但会话变量只提供以前的验证码值,我无法'解决这里的问题。请有人帮我解决这个问题

联系页面

<?php
session_start();
?>
<?php
/**
 * @package WordPress
 * @subpackage naam
 * Template Name: Contactpage*/
get_header(); ?>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function checkEmail(email)
{
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email))
{
return false;
}
else
return true;
}
function checkCaptcha(captcha)
{
var original_captcha = "<?php echo $_SESSION['6_letters_code'] ?>";

alert("original:"+original_captcha+"\n"+"user:"+captcha);
var user_entered_captcha = captcha;
if (original_captcha.localeCompare(user_entered_captcha) == 0)
{
return true;
}
else
return false;
}

$(document).ready(function() {
$("#submit").click(function(){
    var fname = $('#fname').val();
    var email = $('#email').val();
    var phone = $('#phone').val();
    var mesg = $('#mesg').val();
    var captcha = $('#6_letters_code').val();
    var admineml= 'sarath.sarigama@gmail.com';//'<?php echo get_option( 'admin_email' ); ?>';
var err = 0;
        $('#fnamemsg').html('');
        $('#emailmsg').html('');
        $('#mesgmsg').html('');
        $('#phonemsg').html('');
        $('#captchamsg').html('');


if(fname.search(/\S/) == -1)
{
err = 1;
$('#fnamemsg').html('<font style="color:red">Error ! Enter your name</style>');
}
if(checkEmail(email) == false)
{
err = 1;
$('#emailmsg').html('<font style="color:red">Error ! Enter email address</style>');
}
if(checkCaptcha(captcha) == false)
{
err = 1;
$('#captchamsg').html('<font style="color:red">Error ! Enter the captcha</style>');
}
if(mesg.search(/\S/) == -1)
{
err = 1;
$('#mesgmsg').html('<font style="color:red">Error ! Enter  message</style>');
}
if(phone.search(/\S/) == -1)
{
err = 1;
$('#phonemsg').html('<font style="color:red">Error ! Enter  phone number</style>');
}
    if(err == 1)
    {
    return false;
    }
    else
            jQuery.ajax({
                                type: "POST",
                                url: "<?php bloginfo('stylesheet_directory'); ?>/formsub.php",
                                data: {fname: fname, email: email, phone: phone, admineml: admineml, mesg: mesg},
                                success: function(data)
                                {
                                    //alert(data);
                                    if(data=='true')
                                        {
                                        document.getElementById('scs_msg').innerHTML="Submit Successfully";
                                        document.getElementById('scs_msg').style.display="block";
                                        document.getElementById('main').style.display="none";
                                        }
                                    else
                                        {
                                        document.getElementById('scs_msg').innerHTML="Error Please Try again";
                                        }
                                }
                                });
//return true;
});
});
</script>

<section id="container">
    <section class="greyarea">
        <section class="greyareacontainer">
        <section style="clear:both; height:20px;"></section>
            <h1>Contact us</h1>
           <section class="contactBlock">
            <article class="contactBlock_left">
             <?php
            $args = array( 'posts_per_page' => 1, 'offset'=> 0, 'post_type' => 'address' );
            $myposts = get_posts( $args );
            foreach ( $myposts as $post ) : setup_postdata( $post );
            ?>  
                <section class="triblock">
                <h1><figure><img src="<?php bloginfo('stylesheet_directory'); ?>/images/icon1.png" alt="" ></figure>Address</h1>
                <p><?php print_custom_field('address_details'); ?></p>
                </section>
                <section class="triblock">
                <h1><figure><img src="<?php bloginfo('stylesheet_directory'); ?>/images/mobile.png" alt="" ></figure>
                Phone Number</h1>
                <p><?php print_custom_field('ph_no'); ?></p>
                </section>
                <section class="triblock">
                <h1><figure><img src="<?php bloginfo('stylesheet_directory'); ?>/images/mail.png" alt="" ></figure>
                Email</h1>
                <p><a href=""><?php print_custom_field('email_add'); ?></a></p>
                </section>
                 <?php  endforeach; ?>  
            </article>
            <article class="contactBlock_middle">
            <figure><?php dynamic_sidebar('g_map'); ?></figure>

            </article>
            <article class="contactBlock_right">

            <div style="display:none;" id="scs_msg"></div>

            <div id="main">
            <div class="errorBlock">
                    <div id="fnamemsg"></div>
                    <div id="emailmsg"></div>
                    <div id="phonemsg"></div>
                    <div id="mesgmsg"></div>
                    <div id="captchamsg"></div>
                    </div>
            <form action="" method="post" novalidate="" id="commentform" class="clform">

                <label>Your Name</label>
                <input type="text" placeholder="Enter your name" id="fname">

                <label>Enter email address</label>
                <input type="email" placeholder="Email Address" id="email">

                <label>Enter phone number</label>
                <input type="text" placeholder="Phone Number" id="phone">

                <label>Message</label>
                <textarea cols="" rows="" placeholder="Enter your message" id="mesg"></textarea>


                <img src="http://callagylaw.com/wp-content/themes/callargy/captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' >

                <label for='message'>Enter the code above here :</label>
                <input id="6_letters_code" name="6_letters_code" type="text">

                <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>


                <input type="button" value="Submit" id="submit">
                </form>
                <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>
            </div>
            </article>
           </section>



        </section>
    </section>



</section>

<?php get_footer(); ?>

验证码生成代码

<?php 
/*
*
* this code is based on captcha code by Simon Jarvis 
* http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*/

session_start();
//Settings: You can customize the captcha here
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';

//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 0;
$random_lines = 20;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";

$code = '';


$i = 0;
while ($i < $characters_on_image) { 
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}


$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);


/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);

$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'], 
        $arr_text_color['green'], $arr_text_color['blue']);

$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
        $arr_noice_color['green'], $arr_noice_color['blue']);


/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);
}


/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}


/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code); 
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);


/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code'] = $code;

function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);

  return array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
}
?>

1 个答案:

答案 0 :(得分:0)

如果我不是小姐明白你的问题你首先需要以下列形式显示验证码:

<img src="showCaptcha.php" alt="captcha"  />
<br/>
<label >Verify You are not a BOT</label>
<input type="text" name="captcha" placeholder="Enter the text shown above"  required/>

然后当用户提交表单时,您可以使用get captcha值。

简单来说,您首先需要运行验证码脚本,然后您可以检索存储在会话中的验证码值。

希望如果没有请帮助我发表评论,我将很乐意为您提供帮助