Pass php session value to Ajax

2

I need to get a value from a php session that is on a page where I have a contact form and pass through Ajax to another page, on that page I check whether the values are the same or not, but I can not do .

My captcha code creates the session view:

<?php

session_start();

$codigoCaptcha = substr(md5( time()) ,0,9); 
$_SESSION['captcha'] = $codigoCaptcha;  

$imagemCaptcha = imagecreatefrompng("fundocaptch.png");
$fonteCaptcha = imageloadfont("anonymous.gdf");
$corCaptcha = imagecolorallocate($imagemCaptcha,127,82,53);

imagestring($imagemCaptcha,$fonteCaptcha,15,5,$codigoCaptcha,$corCaptcha);

header("Content-type: image/png");
imagepng($imagemCaptcha);
imagedestroy($imagemCaptcha);

?>

I tried to pass the session variable like this:

    <script>
jQuery(document).ready(function(){
    $("#submit").on("click", function(event){
        var nome      = $("#nome").val();
        var email     = $("#email").val();
        var uf        = $("#uf").val();
        var municipio = $("#municipio").val();
        var telefone  = $("#telefone").val();
        var grupo     = $("#grupo").val();
        var assunto   = $("#assunto").val();
        var mensagem  = $("#mensagem").val();   
        var captcha   = $("#captcha").val();
        var captcha_sessao = $("#SESSION['captcha']").val();    

        alert(captcha_sessao);

        var emailFilter = /^.+@.+\..{2,}$/;
        var illegalChars= /[\(\)\<\>\,\;\:\\/\"\[\]]/;

        if((emailFilter.test(email))||email.match(illegalChars)){
            $.ajax({
                type: "POST",
                url: "processo.php",
                dataType: "json",
                beforeSend: function(){
                    $(".processo").html('Gravando contato...');
                },
                data: {'nome':nome,
                       'email':email,
                       'uf':uf,
                       'municipio':municipio,
                       'telefone':telefone,
                       'grupo':grupo,
                       'assunto':assunto,
                       'mensagem':mensagem,
                       'captcha':captcha,
                       'captcha_sessao':captcha_sessao},
                success: function(json){
                    if(json.tipo == "0"){ // erro
                        $(".processo").html("<span class='erro'>"+json.msg+"</span>");
                    }else{ // sucesso
                        $(".processo").html("<span class='sucesso'>"+json.msg+"</span>");
                        cleanFields();
                    }
                }
            });
        }else{
            $(".processo").html("<p style='color:#f25824'>Por favor, informe um e-mail válido.</p>");
        }
        event.preventDefault();
    });

}); 

</script>       

The value of this variable that is the field entered with the generated captcha is sent to the process.php page:

var captcha   = $("#captcha").val();

I do not know exactly how to proceed.

    
asked by anonymous 05.02.2015 / 21:39

1 answer

3

As I understand the question and the code, you are wanting to do a CAPTCHA check. When creating a server side session (with PHP), normally a cookie on the client side is created to store the session ID.

So: Your script saves the captcha in the session:

$_SESSION['captcha'] = $codigoCaptcha;

All you have to do is send the form request via AJAX, with the value of the field that the user typed:

var captcha   = $("#captcha").val();

That is, you do not need to pass on other session or CAPTCHA information. What happens now is this: On the server side, with PHP, you access the session again, and verify that the value saved by the CAPTCHA generator script is the same as the user typed in the field. Example:

process.php

<?php
session_start();

if($_POST['captcha'] != $_SESSION['captcha'])
    die('O captcha digitado é inválido');

The session and its values are created by PHP, and saved on the server. I suggest a look at the session documentation here and here .

    
05.02.2015 / 21:56