简体   繁体   中英

pass a PHP object to an ajax PHP script

How can I pass a PHP object created during my initial page load, to an ajax call?

create-page.php creates my page and has an object expensiveObject . I need to pass expensiveObject to ajax-some-stuff.php .

Option 1: Should I json_encode() the object, attach it to my URL, grab that part of my URL with javascript, and send it back to a PHP ajax?

Option 2: Can I store it as a $_SESSION variable?

I would save it in $_SESSION as this would prevent you from sending data trough internet which should be only processed on server. There would be security issues as well as performance issues.

You might need to implement __sleep() and __wakeup() depending on your object when saving it in $_SESSION

I would like go with Option 1 but after json_encode function i will wrap it again with base64_encode because the json_encode is still not URL-safe string.

The $_SESSION method is good and easy but it depends on your object. If i'm not mistaken, the $_SESSION has maximum size of data can be stored there. - if you're server side session your server memory will consumed - using cookie as session replacement will limit the data size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM