March 11, 2013
No Comments
The function below is one that we have used a few times to allow certain “admin only” assets to be accessed or visible on the front end of a website.
The theory is that the htaccess file in the admin folder will be kept up to date and can therefore be used as the authoritative list of IP addresses that are allowed to access admin assets.
This function is simple enough and should be useful anywhere that you use htaccess to protect a certain folder and would like to implement the same white list rules in other places without having to maintain a duplicate list of authorised IP addresses.
The function also uses static variables – a lesser known bit of PHP functionality which can be a nice easy performance optimisation when working with procedural PHP code. If a function will be called many times and will always return the same result in a single request, you can actually cache that result to a static variable and serve that on any subsequent requests.
function isAllowedIp() {
static $pass = null;
if ($pass !== null) {
return $pass;
}
$pass=false;
$customer_ip = $_SERVER['REMOTE_ADDR'];
$htaccess = file_get_contents('admin/.htaccess');
preg_match_all('%allow from ([0-9.]+)%', $htaccess, $matches);
foreach ($matches[1] as $ip) {
if ($ip == $customer_ip) {
$pass = true; //this is stored statically for perfomance reasons
return true;
}
}
return false;
}
php, By:
Joseph Edmonds
No Comments
Tags:
access,
allow,
authorisation,
control,
deny,
function,
htaccess,
ip address,
list,
php,
procedural,
static,
tip,
variable
If you are having performance issues with your Magento store and you are running on a dedicated or VPS server that you think should be up to the task of running your store properly but you continue to have performance problems then this post is for you.
Having decent server specification is only the first step on the road to having a high performance Magento store. Without proper configuration your server is not going to make the best use of its resources and that could make the difference of literally seconds or even tens of seconds of page load time.
The first and most important thing to check is that you are running a PHP opcode cacher such as APC. Opcode caching takes your PHP source code and compiles it to opcodes and then stores this in a cache. This opcode is actually what is run when people visit your store and the process of creating it, especially if you have a very large application with lots of file (like Magento), can be a real performance bottleneck. This problem is easily resolved by having APC installed and configured. If you are not sure, ring your hosting company and find out and if you don’t have it running, ask them to set it up for you.
The next thing to check is MySQL configuration. The standard MySQL configuration defaults were set when server hardware and memory was a tiny fraction of what it is today and that means that the configuration is generally way too sparse with allocation of memory for caching and other optimisations. Tweaking MySQL can be a little tricky, its definitely not something you should do if you are not sure, but it is well worth getting someone to optimise your MySQL configuration.
After that, the next major performance gain with Magento is to make proper use of block caching. Magento has a brilliant built in feature where every block (page section) can be cached so that next time someone visits the page, the logic used to generate that section of page (for example a best sellers list) does not have to be run, we simply redisplay the cached copy of that block’s HTML.
Beyond these three steps there are still many more things that can be done to improve the performance of your Magento store. If you would like professional help getting the best out of your server and Magento with a view to getting the lowest possible page load speeds then get in touch with Edmonds Commerce today.
magento, By:
admin
1 Comment
Tags:
apache,
apc,
block,
caching,
configuration,
html,
linux,
list,
magento,
opcode,
performance,
server,
sysadmin,
tip,
varnish
March 28, 2012
No Comments
If you use PHP’s list function to quickly extract array values out into dollar variables then you might have an issue where it just doesn’t work for some reason.
The problem is that list only works with numeric arrays. If you are using an associative array (with strings for keys instead of numbers) then list will not work.
There is any easy solution though, simply change:
$array = array('a'=>1, 'b'=>2, 'c'=>3);
list($a, $b, $c) = $array;
To:
$array = array('a'=>1, 'b'=>2, 'c'=>3);
list($a, $b, $c) = array_values($array);
And it will work as you expect.
php, By:
admin
No Comments
Tags:
array,
associate,
bug,
indexed,
key,
list,
numeric,
php,
solution,
string,
values
January 10, 2012
No Comments
If you have a large folder that contains a lot of files but you only want to see what directories are in there, then this little snippet is for you:
ls -d -- */
You may decide to make an alias for it
echo "alias lld='ls -alFhd -- */'" >> ~/.bashrc
September 1, 2011
1 Comment
If you are getting a little bit frustrated trying to figure out why the admin configuration for the number of products to display on a page and the options in the products per page drop down are not working then this is quite possibly your soluiton.
The template file in question is in catalog/product/list/toolbar.phtml
You will see it refererences the block Mage_Catalog_Block_Product_List_Toolbar
So far so good, it all looks like it should be working but it just isn’t.
The issue can be that in your theme’s layout XML file, some values are being set for this with some XML that looks like this:
<action method="setDefaultListPerPage"><limit>10</limit></action>
<action method="setDefaultGridPerPage"><limit>8</limit></action>
<action method="addPagerLimit"><mode>list</mode><limit>10</limit></action>
<action method="addPagerLimit"><mode>list</mode><limit>20</limit></action>
<action method="addPagerLimit"><mode>list</mode><limit>30</limit></action>
If that XML is there, then these actions are being called against your block object with these parameters, effectively overwriting your admin values.
To get the admin values to be respected, you need to comment out or totally remove this XML from your layout, clear your cache and you are in business.
magento, By:
admin
1 Comment
Tags:
count,
dropdown,
layout,
list,
magento,
pager,
problem,
product,
solution,
toolbar,
xml
July 30, 2010
No Comments
If you are working in Linux and need to set some more complex permissions than the straight user, group everyone RWX style, then you can.
Working in Ubuntu, you need to first of all install ACL
sudo apt-get install acl
Now before you can use ACL, you need to enable it on whichever volumes you want to use it on. To do this you have to edit your fstab (which if you do it wrong can cause problems so make sure you back it up!)
In the options column of the fstab for the volume you are working with, you need to add acl, eg:
/dev/mapper/server-root / ext4 errors=remount-ro 0
becomes
/dev/mapper/server-root / ext4 acl,errors=remount-ro 0
Now you need to remount
mount -o remount /
Now you need to check acl is working:
mount | grep acl
you should see a line with acl in there
Now to recursively set an ACL for a particular user giving full access on a folder structure, use this command:
setfacl -Rm u:user:rwX,d:u:user:rwX /path/to/folder
To fully understand ACL, hit the man pages
man acl