1

I'm using this captcha plugin, http://code.google.com/p/cool-php-captcha/ in my website, I'm having the HTML form in my wamp server, and it is submitting it to my live sever located in http://www.example.com/experiments/emaileasy/index.php. Now the problem is, the session return empty array and it is not retrieving when i submit the form in $.ajax, but i can able to see the value if i directly visit the url in my browser. I hardly don't know how to proceed with this,

Here is my index.php file where i'm submitting the form,

<?php
    session_start();

    print_r($_SESSION);die;

    if(isset($_POST))
    {
        if (empty($_SESSION['captcha']) || trim(strtolower($_POST['captcha'])) != $_SESSION['captcha']) 
        {
            header("HTTP/1.0 400 Bad Request");
            header('Content-type: application/json');
            die(json_encode(array('message' => 'Please recheck the captcha')));
        }

        try {
            $conn = mysql_connect('localhost', 'root', '');
            mysql_select_db('expo', $conn);
            $status = mysql_query("INSERT INTO table SET content = '".mysql_real_escape_string($_POST['content'])."'");

            header("HTTP/1.1 200 OK");
            header('Content-type: application/json');
            echo json_encode(array('message' => 'Success!'));
        } catch (Exception $e) {
            header("HTTP/1.0 400 Bad Request");
            header('Content-type: application/json');
            echo json_encode(array('message' => 'Failed'));
        }
    }
?>

Here is my JS file,

$( "#Form" ).submit(function( event ) {
  // Stop form from submitting normally
  event.preventDefault();

  var data = "content="+$( "#content" ).val()+"&captcha="+$( "#captcha" ).val();
  $.ajax({
      type: "POST",
      url: baseurl+'experiments/emaileasy/index.php',
      data: data
  });
});

If i'm submitting the form to localhost it is working properly, but when i submitting it to live server, the session returns empty array.

Here is my .htaccess file,

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Headers X-Requested-With
</IfModule>

Please note that, the session is set through cool-php-captcha which lies on the server. And one more thing i noted is, when the captcha is loaded a session file is getting created and when i post the values a new session file is created in the name of PHPSESSID passing in AJAX requests(I noted this through Firebug response headers).

I'm getting the $_POST values also, but session return empty array when retrieve through AJAX. Please help. Thanks in advance.

Stranger
  • 10,332
  • 18
  • 78
  • 115
  • 2
    I don't see anythingt that actually _writes_ to the session? where is that supposed to happen? – Nanne Feb 10 '14 at 08:20
  • where do you set sessions ? and do not use mysql_* functions, search stackoverfollow and google for mysql_* – Shahrokhian Feb 10 '14 at 08:20
  • you seem to be setting your session variables on localhost, and trying to retrieve them on the server, this won't work. – CodeBird Feb 10 '14 at 08:22
  • Wow RezaSh, thanks for the heads up! lol. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php Very useful post from NullPoiиteя. Unrelated to this post slightly, but truly a good read. But yes, you appear to be missing the actual setting of the session variables, they aren't simply duplicate of the post and get variables. – TheOneWhoPrograms Feb 10 '14 at 08:26
  • @Nanne It is set through **cool-php-captcha** plugin. – Stranger Feb 10 '14 at 08:34
  • @RezaSh It is set through **cool-php-captcha** plugin. – Stranger Feb 10 '14 at 08:34
  • @CodeBird No both setting and retrieving is on the sever only. Please check my edited question. – Stranger Feb 10 '14 at 08:35
  • can we see where are you setting you session variables, and where is that code stored? – CodeBird Feb 10 '14 at 08:40
  • @CodeBird The session is created in `experiments/emaileasy/captcha/captcha.php` through the captcha plugin. – Stranger Feb 10 '14 at 08:43
  • @Udhay you're saying that the original form is on your localhost, so you're actually including the captcha there, so the session is created on your localhost, even though the file is on the server – CodeBird Feb 10 '14 at 08:45
  • @CodeBird Oh is it? But the captcha is created on my server only and we are retrieving also in the same server. Only the form is in localhost. – Stranger Feb 10 '14 at 08:51
  • @CodeBird How to handle this? – Stranger Feb 10 '14 at 09:04
  • I don't know, as I don't understand your setup completely (it seems too complicated). Try putting your form on the same server where the captcha is, and running it there. – CodeBird Feb 10 '14 at 09:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47152/discussion-between-udhay-and-codebird) – Stranger Feb 10 '14 at 09:23
  • sorry was away from my computer, in the chat now... – CodeBird Feb 10 '14 at 09:46

0 Answers0