开发者

Pass a resource object to PHP from Jquery?

开发者 https://www.devze.com 2023-02-20 06:50 出处:网络
I\'m trying to pass an imap_conection (PHP Imap Library) using Jquery.Post doesn\'t seem to work - is there something else I should try?

I'm trying to pass an imap_conection (PHP Imap Library) using Jquery. Post doesn't seem to work - is there something else I should try?

PHP

$conn开发者_运维知识库ection = imap_open($mailServer,$login,$password);

jQuery

    var dataString='email=<?php echo $email_address; ?>&connection=<?php echo $connection; ?>&password=<?php echo $password; ?>&server=<?php echo $server;?>&daysago='+daysago+'&num='+num;
    $.ajax({
       type: "POST",
    url: "search.php",
    data: dataString,

Right now, Firebug's telling me the $_POST['connection'] variable is Resource id #57

Edited to add

Main Issue

I am connecting to imap email addresses using the PHP Imap Library. When I try to reconnect multiple times with the ajax (doing multiple keyword searches), I tend to lose connections to certain email providers (mainly Hotmail), so I'm trying to centralize one connection so I don't lose connections - which is why I'd like 1 connection resource and to pass the connection. Is there another possible solution

The reason I use multiple ajax calls/imap connections is that I want to make the data available to users as fast as possible. This searches usually take about 8-10 seconds each.


$connection is of a special php "type" called a resource. These are created by functions like mysql_connect, fopen and imap_open and represent some kind of state in an underlying C library. These resources are only valid for the duration of the page execution. As soon as PHP has finished executing the page the resource is released. That's the first part of the problem.

The second part is that when you print a resource it is converted to a string. This string representation has lost the underlying state of the resource, and cannot be converted back to the resource. Thus there's no way of doing what you're trying to do.

What you need to do is to reestablish the connection to the imap server in search.php, and drop the connection parameter from the ajax call.


This is because you shouldn't include &connection=<?php echo $connection ;?> to your URL, because it's a resource, and when you try to echo it into the javascript, it gets converted to a string: Resource id #57. You can't pass a PHP resource using JavaScript.


You have 2 main problems here:

  1. When you're trying to echo the resource object, you're calling its __toString() method. This will usually print a very generic description of the object, as you've seen here. The proper way to convert an object for passing elsewhere is using the serialize() function. See the docs for it.
  2. Even if you did try serializing it, it would fail. Resource objects are very state-dependent, and thus cannot be passed like this. It can be used in the script that opened it, and that's about it. See the doc above for more info.

As an added bonus:

You're not escaping any of the values in your dataString, which could easily lead to values being misinterpreted if there were a ? or & in it. You should either run escape on each value being formed, or do something like this:

var data = {
  username: '<?php echo $username ?>',
  password: '<?php echo $password ?>'
};

$.ajax({
  type: "POST",
  url: "search.php",
  data: data,
  ...
});

jQuery will take the object and form the data string for you, and do the proper escaping. Much easier to read, easier to put together, and less easy to make a mistake.

0

精彩评论

暂无评论...
验证码 换一张
取 消