php


12
Nov 09

How to install Google Affiliate pixel tracking code in Magento

Took me a day of scouring the web to figure out how to get this to work. After adding this code do a test order and view source and confirm it worked.

#copy and paste to the bottom of
#app/design/frontend/default/theme_name/template/checkout/success.phtml
$companyID = "K123456"; #provided by Google
$category = urlencode("Your product category");
$lastorderid = Mage::getSingleton('checkout/session')->getLastOrderId();
$_order = Mage::getModel('sales/order')->load($lastorderid);
$orderID = $this->getOrderId(); #get the order ID
$aProduct = array();
$totalAmt = 0;
$prdsku = "";
$prdnm = "";
$prdqn = "";
$prdpr = "";
$prcatid = "";

###########store order data in array of objects###########
foreach ($_order->getAllItems() as $item) {
	$oProduct = new stdClass();
	$oProduct->sku = $item->getSku(); #sku
	$oProduct->price = $item->getPrice(); #price
	$totalAmt = $totalAmt + ($oProduct->price * $item->getQtyOrdered()); #total amt (excl.tax/shipping)
	$oProduct->qty = $item->getQtyOrdered(); #quantity
	$oProduct->name = $item->getName(); #product name
	$oProduct->category = $category;
	array_push($aProduct, $oProduct);
	$oProduct = NULL; #nuke it
}
###########build query string variables###########
for ($i=0; $i < count($aProduct); $i++) {
	if ($i == count($aProduct)-1) {
		$prdsku .= urlencode($aProduct[$i]->sku);
		$prdnm .= urlencode($aProduct[$i]->name);
		$prdqn .= urlencode($aProduct[$i]->qty);
		$prdpr .= urlencode($aProduct[$i]->price);
		$prcatid .= urlencode($aProduct[$i]->category);
	} else {
		$prdsku .= urlencode($aProduct[$i]->sku) . "^";
		$prdnm .= urlencode($aProduct[$i]->name) . "^";
		$prdqn .= urlencode($aProduct[$i]->qty) . "^";
		$prdpr .= urlencode($aProduct[$i]->price) . "^";
		$prcatid .= urlencode($aProduct[$i]->category) ."^";
	}
}
?>



16
Oct 08

How to setup a linux cronjob in 3 steps

Since web apps are executed only after an HTTP request, you may have situations where scripts need to run in the background on a scheduled basis. On Linux, to do this you’ll need to get to know the crontab. The way it works is kind of like an airport. The service is always running, however planes only depart at their scheduled times. The crontab is kinda like that, a daemon that runs constantly in the background and checks once a minute to see if any jobs need to be executed.

Let’s say you have a script located at http://www.acme.com/myscript.php that needs to run the first of every month at 9am. Here’s how you’d set it up:

1. Look for a file in your /etc folder called crontab.
2. You need to install a new crontab job, to do this type crontab -e.
3. Now the tricky part, the syntax:

and here’s how that translates in our example:

0 9 1 * * curl http://www.acme.com/myscript.php

Lets break it down. 0 is for the minute, 9 is the hour – so 9 hours & 0 minutes which is 9am. Day of the month, 1 – easy. We want to do this for every month & week, which is accomplished using the wildcard or * character. Next comes what we want to execute. It’s not enough to just type the URL of the script, you’ll need to use the CURL system function to make the HTTP request for you.

Here is some more information on CURL & Cron jobs for your reading pleasure, hope this tutorial helps!


10
Apr 08

How to authenticate against Active Directory using PHP

If you are like most companies out there, your IT department probably has everyone signing on to a Microsoft Active Directory domain. Wouldn’t it be nice to support single sign on, avoid all those seperate username/passwords littered across your intranet & save yourself some database programming? Here’s how.

First things first, you’ll need to make sure you have LDAP support installed. You can double check this by creating a php info page:

echo phpinfo();

If you run this page & see the LDAP support in there, you are good to go. If not, you’ll need to recompile PHP with ldap support by adding the –with-ldap[=DIR] option to the configure options. For sake of brevity i’ll assume you understand how to make an HTML login form. Once you have created this, you will want to capture the username & password that the user submits. Now you’ll need to connect to the ldap server:


session_start(); #make sure this is at the top of your PHP file.
$adServer = "127.0.0.1"; #replace with your AD server ip/hostname
$ldapconn = ldap_connect($adServer)
or $this->msg = "Could not connect to LDAP server.";

Now that we’re connected, you can attempt to authenticate the username/password submitted, which will return a boolean value:


$ldaprdn = $adServer . "\\" . $_POST["email"];
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $_POST["password"]);


if ($ldapbind) {
$msg = "Successfully Authenticated";
$_SESSION['email'] = $email;
$_SESSION['password'] = $password;
return true;
} else {
$msg = "Invalid email address / password";
return false;
}

Now that you have stored the authenticated username/password in the users session data, you just need to run this same script before each page loads as you would with mysql user authentication.

From my experience, half the battle was getting my app to talk to the AD server, so best to be clear with your IT guys as to what you are attempting & what you need to minimize frustration . This was a pretty quick overview, so if you have any questions feel free to drop a comment and i will be happy to help. You can also check out the great documentation @ php.net.


31
Mar 08

Phpmyadmin error: “Cannot start session without errors”

I have been trying to get phpmyadmin working on my Fedora 8 server i’ve been setting up, and struggled for the past day or so on this error:

Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.

Turns out it’s very easy to fix. First thing you’ll want to do is have a look at your error logs. For me this was in /var/log/lighttpd/error.log (i’m using lighttpd instead of Apache). Have a look at your error log and you will probably see errors referencing permission errors writing to your session directory.

Simply chmod 777 that directory and you should be good to go.