Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 167 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I set a cookie in a wordpress ajax request handler?

#1
I am trying to set a cookie in a wp ajax request handler without any success.<br>Hope someone can help me.<br><br>
Here is client-side code:<br>

var foo = $('#foo');
$.ajax({
url: 'http://127.0.0.1/wordpress/wp-admin/admin-ajax.php',
type: 'post',
dataType: 'json',
cache: 'false',
data: { 'action' : 'foo', 'foo' : foo.val(), 'nonce' : foo.data('nonce') },
success: function(data) { console.debug(data) },
error: function() { console.error('fail') }
});

Here is the plugin:

function foo() {
check_ajax_referer( 'foo-my-nonce', 'nonce', 'what?!' );

if (isset($_POST['foo'])) {
$dir = pathinfo($_SERVER['REQUEST_URI']);
$dir['dirname'] = $dir['dirname'].'/';
setcookie('foo', $_POST['foo'], time()+62208000, $dir['dirname'], $_SE RVER['HTTP_HOST']);

$output = array('response' => 'success', 'message' => 'have fun');
}
else
$output = array('response' => 'failed', 'message' => 'you are a loser');

header("Content-type: application/json");
echo json_encode( $output );
exit;
}

add_action('wp_ajax_nopriv_foo','foo');
Reply

#2
Ok guys I fixed that.
In order to make it working you must set the cookie path to "/" (the root) of the website, instead of the current 'post' or 'dirname':

Instead of:

`setcookie('foo', $_POST['foo'], time()+62208000, $dir['dirname'], $_SERVER['HTTP_HOST']);`

Use:

`setcookie('foo', $_POST['foo'], time()+62208000, '/', $_SERVER['HTTP_HOST']);`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through