Magento redirect

Sometimes there is a need to do a redirect from inside a Magento page where you might not know if headers are sent or not – Or maybe you wish to make a new redirect function where you don’t have to worry about writing different code if the headers are sent or not.

This is a way to do a redirect where a javascript redirect is used if headers have been sent (and thus it is no longer possible to use a header redirect)

if (!headers_sent()) {
 
	Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl() . 'portal/');
 
}
 
else {
 
	$baseUrl = Mage::getBaseUrl() . 'portal/';
 
	print '<script type="text/javascript">';
 
	print "window.location.href = '{$baseUrl}';";
 
	print '</script>';
 
}

And the function headers_sent() is a php function so that can of course be used outside of Magento in any php scrip where a header location is nice but not always possible.

Keep ie6 installed (don’t update ie to newer versions)

For testing I like to keep older versions of Internet Explorer installed, here are some notes on how to disable the updating of Internet Explorer to newer versions.
I only recommend doing this on a machine that should keep Internet Explorer 6 in order to do development testing.

  • Disable Internet Explorer 7 Open up a registry editor and check if the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Setup\7.0” exists, otherwise create it (the key is the tree structure in regedit).
    Now enter a Key name “DoNotAllowIE70” as a DWORD and give it the value “1”.
    This will keep Internet Explorer 8 from installing on this computer.
  • Disable Internet Explorer 8 Open up a registry editor and check if the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Setup\8.0” exists, otherwise create it (the key is the tree structure in regedit).
    Now enter a Key name “DoNotAllowIE80” as a DWORD and give it the value “1”.
    This will keep Internet Explorer 8 from installing on this computer.
  • Disable Internet Explorer 9 Open up a registry editor and check if the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Setup\9.0” exists, otherwise create it (the key is the tree structure in regedit).
    Now enter a Key name “DoNotAllowIE90” as a DWORD and give it the value “1”.
    This will keep Internet Explorer 9 from installing on this computer.
  • Disable Internet Explorer 10 Open up a registry editor and check if the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Setup\10.0” exists, otherwise create it (the key is the tree structure in regedit).
    Now enter a Key name “DoNotAllowIE10” as a DWORD and give it the value “1”.
    This will keep Internet Explorer 10 from installing on this computer.
  • Disable Internet Explorer 11 Open up a registry editor and check if the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Setup\11.0” exists, otherwise create it (the key is the tree structure in regedit).
    Now enter a Key name “DoNotAllowIE11” as a DWORD and give it the value “1”.
    This will keep Internet Explorer 11 from installing on this computer.
  • The values mean the following:

    • When the key value name is not defined, distribution is not blocked.
    • When the key value name is set to 0, distribution is not blocked.
    • When the key value name is set to 1, distribution is blocked.

    note This article is updated as new versions are released.

    If you need to do this on several computers then you can download Toolkits to do this from Microsoft (warning: read the instructions and verify that you ran it correctly on the first computer you run it on):

    PHP: Converting a bool value to a string value

    The first idea that comes into mind for doing this is to simply make a typecast of the boolean variable to a string and use that (which works)

    $testVar = false;
    echo (string)$testVar; //will output a empty string (as that evaluates to false)
    $testVar = true;
    echo (string)$testVar; //will output 1 (as that evaluates to true)

    but what if the requirement is that the string should say true or false

    $testVar = false;
    echo $testVar ? 'true' : 'false'; //will output false
    $testVar = true;
    echo $testVar ? 'true' : 'false'; //will output true

    (While the above example might seam useless I recently had to do this for a db insert)

    Different line-endings on diffrent OS

    Windows, Linux/Unix and Mac have different line-endings on text files.

    A quick overview on the endings are:

    • Windows-style line endings are CRLF ( \r\n or hex 0D0A )
    • Mac-style line endings are CR ( \r or hex 0D )
    • Unix-style line endings are LF ( \n or hex 0A )

    A HEX editor can be used to double check that the correct endings are written.

    PHP: Static functions in a Class

    Static functions can be used without instantiating the object.

    A quick example of a static function is shown below as well as a comparison with a non static function.

    A quick example of this (in PHP5):

    class TestClass{
    	static function staticFunction(){
    		echo "static function called";
    	}
     
    	function nonStaticFunction(){
    		echo "nonstatic function was called";
    	}
    }
     
    TestClass::staticFunction(); //will echo static function called
     
    $testClass = new TestClass();
    $testClass.nonStaticFunction(); //will echo nonstatic function was called
    unset($testClass);

    PHP: converting a uuid stored as BINARY(16) in mysql to RFC 4122 format

    If you are storing uuid’s are BINARY(16) in the database, then the following will turn the stored id to the “original” format.

    $uuidReadable = unpack("h*",$uuid);
    $uuidReadable = preg_replace("/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/", "$1-$2-$3-$4-$5", $uuidReadable);
    $uuidReadable = array_merge($uuidReadable);
    echo "uuid is " . $uuidReadable[0];

    PHP5: Quick on timing a script

    Sometimes it is nice to know how long a scrip took to execute (or a part of a script).
    This is a simple way to time a script in php.

    $time_start = microtime(true);
     
    usleep(500);
     
    $time_end = microtime(true);
    $time = $time_end - $time_start;
     
    echo "run time was " . $time . " seconds";

    SimpleXML tags with – in the name

    Sometimes a xml file will have tags with “-“s in the name.
    It is not to hard to parse this using simplexml, just use the tagname inside curly brackets like: “{tag-name}”

    <?xml version="1.0" encoding="utf-8"?>
    <medprod xmlns:npl="urn:schemas-instance" version="129">
      <statuses>
        <mpa_status xmlns:mpa="urn:schemas-mpa" datastatus="valid" procstatus="reviewed"/>
      </statuses>
      <mpa_pharmaceutical-form-group-lx xmlns:mpa="urn:schemas-mpa" v="OSD"/>
    </medprod>

    Let’s assume we load the above in a SimpleXML object called $simplemedprodXml

    $simplemedprodXml= simplexml_load_string($xmlString);
    echo (string)$simplemedprodXml['version'];
    echo (string)$simplemedprodXml->statuses->mpa_status['datastatus'];
    echo (string)$simplemedprodXml->statuses->mpa_status['procstatus'];
    echo (string)$simplemedprodXml->{'mpa_pharmaceutical-form-group-lx'}['v'];

    .htaccess to allow google ads (the indexing bot) [or limit user-agents via .htaccess]

    Sometimes there is a need to only allow limit access to a site using a .htaccess file.
    If you still wish to be able to serve Google adsense on this page then the following example is good for this (yes the allow line should be changed to suit your needs)

    # Google_ads agents
    BrowserMatchNoCase Mediapartners-Google good_pass
    BrowserMatchNoCase Adsbot-Google good_pass
    <LIMIT GET POST>
    order deny,allow
    deny from all
    allow from 123.456.789
    #Google ads
    allow from env=good_pass
    </LIMIT>