PHP website to pass specific information only on request

I'm having trouble trying to come up with a smart way to do what I need. Basically, I have a common site created by a custom CMS editor, everything is fine. I want this to happen when a user is in our CMS editor using an AJAX request in the background, he will quickly request the page that the user is viewing and return special instructions depending on the data received. For instance:

There are some special instructions for the CMS on the contacts page, it is possible that everything should have a title and subtitles. Thus, these triggers are encoded in contact.php, but are not displayed to ordinary users viewing the website. There are no fields.

But when the user is in our CMS, I want him to send a special request http://www.something.com/contact.php , and contact.php will know his CMS is knocking on the door, and will respond with special instructions. Idiot, this is JSON, or XML, something, I'm not fussy.

The real problem I am facing is that contact.php displays both versions of itself. I can return a JSON string, but jQuery Ajax does not know how to handle it, since there are other elements in the response. If jQuery-> Ajax can ignore the whole page and just capture JSON, this should be fine.

<html> <body> <h1>blah blah</h1> ...etc ...etc <script>var requiredSettings = {[json blah blah blha ]};</script> <p>loreum ipsum etc..</p> </body> </html> 

This is what I need to work with, how can a jQuery-> Ajax request ignore all wqrong and html garbage headers and use JSON.

I am very open to other methods if contact.php can remain intact and visible to users, but play a double load and report to the CMS when the CMS requests information.

CMS and contact.php will be on different servers.

Thank you for your time.

+4
source share
1 answer

In your PHP file, you can always:

 if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { return jsonencode(JSON); die(); } HTML site here. 

This will return json if the page was requested using ajax, and go to the HTML part (or something else) if it wasn’t.

+5
source

Source: https://habr.com/ru/post/1441696/


All Articles