News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

PHPfile not part of SMF

Started by RuudN, April 12, 2011, 04:40:30 PM

Previous topic - Next topic

RuudN


I am trying to make a mod for the first time, everything is going well. It's a mod to give topics thumbs up or thumbs down. Now I am about to use AJAX so people can click on the thumbs down or thumbs up images, without the need to refresh the page to see how many thumbs the topic has.


I am using sendXMLDocument() to get data from a PHP file which handles the request to add one thumb and returns the new amount of "thumbs". (Example: user clicks on thumb up button, php file updates the mysql database to (currentThumbs + 1) and returns (currentThumbs + 1).) If I do:
if (!defined('SMF'))
die('Hacking attempt...');

at that PHP file, it returns hacking attempt. This is because SMF didn't request that file. This is a problem, because I do want to use the SMF variables on that PHP page. How can I make the PHP file part of SMF?

I hope that the problem is clear and that someone can answer my question!

Thanks in advance,

Ruud

Arantor

Well, it depends, there's two ways you can do it.

If you make it part of an action, and callable through index.php?action=mynewaction (like other SMF actions), make sure to add it in index.php, and it'll work as intended.

If you make it a standalone file, skip that defined check and just require SSI.php as the first line of the script to bootstrap SMF and set up the connection and other things.
Holder of controversial views, all of which my own.


RuudN

I added the file to the index.php like this:
'thumbs' => array('thumbs/thumbs.php', 'Thumbs'),

Then I tried to change
sendXMLDocument("' . $sourceurl .'/thumbs/thumbs.php", "", refine);
To
sendXMLDocument("' . $boardurl .'/index.php?action=thumbs", "", refine);

That didn't work, I don't get anything back from the phpfile now. Simply adding the file to the index.php without changing the sendXMLDocument() doesn't work either.

That leaves me with using SSI (which isn't bad, don't get me wrong). But  is there really no other option?

Arantor

Well, you're asking me to figure out a problem with your code in thumbs.php without showing it to me... until I develop telepathy, or the ability to hack into your computer to see the file, or you upload it here, I can't help you figure out what the problem is.

Also, what is 'refine'?
Holder of controversial views, all of which my own.


RuudN

This is the function I am talking about
function thumbs(id,othervariable){

function test(myDiv) {
if (typeof myDiv == \'string\') myDiv = document.getElementById(myDiv);
return myDiv;
};

sendXMLDocument("' . $sourceurl .'/thumbs/thumbs.php", "", refine);

function refine(data)
{
test(id).innerHTML = data.getElementsByTagName(\'response\')[0].getElementsByTagName(\'message\')[0].firstChild.nodeValue;;
}

}


And thumbs.php contains this:
<?php
if (!defined('SMF')){
$t = 'Hacking attempt...';
}else{
$t = 9000;
}

header('Content-type: text/xml; charset=ISO-8859-1');
echo
'<?xml version="1.0"?>
<response>
   <message>';

echo $t;

echo '</message>
</response>';
?>


Currently I did add thumbs to the index.php file.

I am really embarrassed of myself for not giving you enough info and also posting in the wrong board, thanks for your patience though.

Arantor

The reason it fouls up is because it ends up trying to bootstrap the rest of SMF, including loading the theme, and then going to spectacularly crash, burn and fail because it won't have a template to load, never mind the fact you're directing it to call a function that doesn't exist.

Your thumbs.php file should resemble the others, and stop the template loading at the end.

<?php
if (!defined('SMF'))
$t = 'Hacking attempt...';

function
Thumbs()
{
$t = 9000; // or however you expect this to work
header('Content-type: text/xml; charset=ISO-8859-1');
echo '<?xml version="1.0"?>
<response>
   <message>';

echo $t;

echo '</message>
</response>';

obExit(false);
}
?>


So, the action=thumbs will load this file (presuming you put it in Sources/thumbs/thumbs.php as you've indicated in the loader), and try and call Thumbs() which didn't exist - now it does.

The obExit(false) tells it to end output buffering and not load the rest of the theme - which is how you get raw XML out of it. And note that you should be using:
sendXMLDocument(smf_scripturl + "?action=thumbs", "", refine);
Holder of controversial views, all of which my own.


RuudN

Thank you for that answer, not only did that fix the problem it really helps me to understand more of the basics of SMF forums.


Advertisement: