Page 1 of 1

Including a PHP script in template?

Posted: Sat Jul 02, 2011 9:38 am
by laits
I have read up on the forum about how to do this, and understand that I need to put my php inside either {php} tags (and enable in config) or put in a UDT.

The first problem i'm having is that every time I try and submit either I get a 403 Forbidden. But it seems only when I am including php in either the template or UDT???

Obviously I'm falling at the first hurdle here...

Secondly the developer ( for a real estate listing website) has said:-
The code contained in the index.php file will need to be placed in the containing element where you wish the results to be display except for the first and last lines which will need to be placed on the first and last lines of your document.
These first and last lines are:-

Code: Select all

<!--place this on line 1-->
<? ob_start(); ?>
<!--place this on line 1-->

Code: Select all

<!--place this on last line-->
<? ob_flush(); ?> 
<!--place this on last line-->
Should I just wrap them in {php} tags and put then in on first and last lines of template (respectively), or will this not work.

Thirdly (and lastly) there is also a config.php for this script, where should i put this and how do i link it. in the first line of the main script there is the line:-

Code: Select all

require("config.php");
if i make this a full path to the directory where i place the config.php, will this work? If not how can I do this?

I obviously can't try the trial and error method, as I can't even complete step one :-(

I have built about 50 sites on cmsms and never needed an ounce of php knowledge (I know a little bit), so please forgive my lack of detailed understanding of both php and trying to intergrate it into cmsms.

Worst case scenario I'll have to build a page outside of the cms, but thats a bit of a cop out in my opinion.

Any help/direction much appreciated.

Rich

Re: Including a PHP script in template?

Posted: Mon Jul 04, 2011 8:42 am
by laits
Ok, though trial and error, I have worked out that i can save the UDT as long as it has no refererence to curl it seems.

Can anyone tell me what maybe wrong with this script and why I can't turn it into a UDT

Code: Select all

require("config.php");
    
	// find out request method to use
	switch($_SERVER['REQUEST_METHOD'])
	{
		case 'GET': $the_request = &$_GET; break;
		case 'POST': $the_request = &$_POST; break;
		default:
	}
	
	// check which page to request from
    if ($the_request['pid'] . "" != "")
	{
        $url = $url . "Property.aspx?";
	}
	else
    {
        $url = $url . "Default.aspx?";
    }
    
	// get all the params and build the url
	foreach($the_request as $item => $value)
	{
		$url = $url . $item . "=" . urlencode($value) . "&";
	}
    
	// add api key if not supplied
	if (strpos($url, "apikey=") < -1)
	{
		$url = $url . "apikey=" . $apikey . "&";
	}
    
	// add eaid if not supplied
	if (strpos($url, "eaid=") < -1)
	{
		$url = $url . "eaid=" . $eaid . "&";
	}
	
	// session key stuff
	if (strpos($url, "sessionGUID") < -1)
	{
		if($_COOKIE["sessionGUID"] . "" == "")
		{
			// if no session cookie generate guid and make cookie
			$guid = getGUID();
			setcookie("sessionGUID", $guid, 0);
			$url = $url . "sessionGUID=" . $guid;
		}
		else
		{
			// session cookie exists so use that value
			$url = $url . "sessionGUID=" . $_COOKIE["sessionGUID"];
		}
	}
		
	// add a BASE HREF tag so stylesheet, images and scripts are loaded
	//echo '<BASE HREF="' . $url . '">';
    
	// fake user agent to not get blocked
	ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
	
	// get file methods
	
	if($method == "curl")
	{
		// create curl resource 
		$ch = curl_init(); 
		// set url 
		curl_setopt($ch, CURLOPT_URL, $url); 
		// return the transfer as a string 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		// $output contains the output string 
		$output = curl_exec($ch); 
		// close curl resource to free up system resources 
		curl_close($ch);
	}
	
	if($method == "fopen")
	{
		// open url
		$handle = fopen($url, "rb");
		// get the contents of the url as a stream
		$output = stream_get_contents($handle);
		// close fopen resource
		fclose($handle);
	}
	
	if($method == "file")
	{
		$output = implode('', file($url));
	}
	
	if($method == "file_get_contents")
	{
		$output = file_get_contents($url);
	}
	
	// end get file methods
	
	// replace links in html output and write to the screen
	$output = str_replace($urlToLookForMain, $urlToReplaceWithMain, $output);
	$output = str_replace($urlToLookForProperty, $urlToReplaceWithProperty, $output);
    echo($output);
	
	// generate guid function
    function getGUID()
	{
		$theGuid = uniqid(uniqid(), true);
		return $theGuid;
	}
	
I'm pretty stumped here, but then again I work predominantly with HTML & CSS, and don't fully understand PHP scripting, or even what curl is?

Any hints or help greatly appricieated!