Saving Form Data Twice in CakePHP
CakePHP makes it increbibly easy to insert data into MySQL tables with the use of their inbuilt form helper and and save function however one question I have seen alot is how to save form data more than once equating to multiple row inserts? The answer is actually quite simple.
When you use the CakePHP save method, CakePHP holds on to the last primary ID it created, and so if you try using the save method again, in the same session.. nothing happens, as the row already exists as far as Cake is concerned. You need to make Cake forget about the last primary ID it's holding.
$this->Whatever->create();
$this->Whatever->save($this->data);
$this->Whatever->create();
$this->Whatever->save($this->data);
The code above runs the create() method on the Whatever model reseting any primary ID Cake was holding for it. Then it runs the save method saving the form data to the Whatever model. It then runs the create method again, resetting the primary ID and then saves the form data again, giving you two rows in your table with the same data all bar the primary key.
Of course you would more than likely put your save in a loop of some kind if you were planning on multiple saves.
$inserts_required = 1;
while ($inserts_required <= 10) {
$this->Whatever->create();
$this->Whatever->save($this->data));
echo $inserts_required++;
}
The above will insert 10 rows with the same data, bar the primary ID.
Spam Sending Clients on Shared Cpanel Servers
Recently one of our reseller Cpanel clients on one of our shared servers managed to have the shared server IP blacklisted in two popular blacklists by sending out spam email using an automated mailing script. As you may know, when one client gets a shared IP blacklisted all clients on that IP are then effected so this was a priority case for us.
Believe it or not many of the most popular blacklists such as SpamCOP are more than happy to remove an IP as a spam source providing you can prove you are not a spam trader. Some of the blacklists you must manually request an IP to be removed however others will automatically remove an IP if it does not get any complaints or trap any spam from it within a number of hours.
Tips to Recover from your Shared Cpanel Server Blacklisting
- Of course the first step was to remove the offending auto mailing script and shutdown the said client's hosting account. Done.
- Next, search the blacklists for your shared IP. There are some good free tools out there to help you with this. Such as MX Toolbox.
- Contact each blacklist your IP appears in and advise you have taken steps to remove your offending client and increase mail security. Await further instruction from each blacklist.
On Spam Prevention using WHM
As the shared IP in question here belonged to a WHM server we run, we will cover some WHM settings that could go a long way in the fight against clients who like to spam.
Load up WHM as root and click through to Main >> Server Configuration >> Tweak Settings and scroll down to mail. There are two very useful settings here.
The maximum each domain can send out per hour
This is quite self explanatory, set it to a low number rather than the default of 0 which is unlimited and each domain is seriously halted when it comes to sending bulk mails.
Prevent the user "nobody" from sending out mail to remote addresses
Some poorer auto mailing scripts will send mail out as the user nobody, set this setting to enabled and this decreases the risk too.
We hope these notes can be of use to individuals and companies alike in the ongoing fight against spam.
Don't Break Your Back Coding Pages With DIV's instead of Tables
The inspiration behind this post was a blog post by Stefan Mischook of killersites.com. Stefan has been a great source of business inspiration for many when it comes to developing commercially. The blog post in question can be found on Stefans blog, found here.
Up until fairly recently one could lay their page out using HTML tables, and providing CSS was being used amply, nothing more would be said. However today is a different story. These days, any serious web developer would have asked themselves if using DIV's to layout the elements of a page rather than tables is worth doing.
A little background on HTML Tables
HTML tables were never intended to entirely control a web page's layout however tables rapidly become the choice due to their ease to implament. I still today have not found an editor such as Dreamweaver or Frontpage that offers as much functionality on a plate for DIV's as they do tables and this could well explain how tables dominated view code for so long.
What are HTML Tables to be used for?
The intention, as I see it, is that tables were used to display 'table data'. So league tables, various other kinds of data - tables, regardless as to if they are on the web or out in the real world, have always been used to display data in rows and columns.
What's so good about a DIV?
It is said that search bots can crawl a page much more efficiently using if that page is coded using a DIV layout over a tabled layout. I'm yet to see certified proof that this statement is correct althought I'm in no disbelief that DIV layouts have many other advantages.
Advantages to a DIV'd layout over a TABLE'd layout as I see it ...
- Freedom - Use DIV's and valid CSS to layout your page and you will find much more freedom in terms of positioning of elements and data.
- Rendering Speed - I have seen a few comparisons done from browser to browser recently on how fast a page loads, it seems they are all gearing up on DIV's and how quickly they can load them.
- AJAX - AJAX can easily update table data however when it comes to elements, using DIV's usually means less view code.
- No Flaming - you don't get flamed on forums when you post links to your work.
Now I'm sure there are heaps more however the first three I listed there I believe to be the most beneficial. The last one does not bother me. Though it does bother me that manors seem to be decreasing as DIV layout popularity increases.
Should I Use DIV's Now Then?
It seems unquestionable that DIV layouts are the future, and where feasable I will personally use DIV's to layout my pages rather than Tables however the choice is totally at the developers discretion. If you are happy developing your pages using table layouts and your clients has not specified a DIV layout then do it! Just make sure that either way your XHTML and CSS validates to W3C standards, if it does - sophisticated search bots such as Google's will have no problem crawling your site.
No doubt this post will raise some grumbles from new-starters through to experienced life-long developers. Although I'm not pro HTML Tables for page layouts going forward I'am pro choice - developers should stick together and learn from one another, not flame and hassle each other for our preferred practices.
Links on Images in the CakePHP Framework
Introduction: I have seen a fair number of posts on the cakePHP google group and also on a few blogs regarding how to make links in views that are images using the cakePHP helpers. There are ways to fudge this and hard code paths to images and links but when you start swapping between /pages and /controllers this all gets very messy and is not using what cakePHP provides so I thought I would write a quick tutorial on this. Actually it's more like a few notes with an example - this is a real easy task, and quite obvious once you consider what we are doing.
Assumptions: I'm assuming your using cakePHP 1.2 here if your not, go get it
If you can't upgrade then just read this example code below then check the 1.1 manual for 'image' and 'link' and you should be able to work it.
Step 1: Displaying an Image on a View or Layout
O.K as you may or may not know, to display and image on a layout or view in correct the correct cake manor just ...
echo $html->image('home.gif');
Obviously above, change 'home.gif' to your image.
Step 2: Making a HREF Link on a View or Layout
Another one you may already know...
echo $html->link("Home", array('action' => 'features'), array('escape' => false));
And again, change 'Home' to your link text and 'features' to your action in your controller. The above example will link the text 'Home' to the 'features' action of the currently active controller. If you have this on a view in the /pages cake directory it will link to /pages/features. This is no good if you wan't to link to another controller, however this is easy too ...
echo $html->link("Products", array('controller'=>'products', 'action' => 'features'));
Above we are also specifying the controller name. The above example will link the text 'Products' to the controller 'products' and the action 'features'. ie /products/features.
Step 3: Coming image and link for a linked image
O.K the bit of code you came to this page for ...
echo $html->link($html->image("products.gif"), array('controller'=>'products', 'action' => 'features'), array('escape' => false));
As you can see, from a PHP point of view it is as simple as including a function within another function. So here, the image 'products.gif' links to the 'products' controller and the 'features' action of that controller. You may also noticed we have added escape = false. If you do not specify this as fasle, or you specify it as true then the raw html for the image will be out printed to the view as the link instead of the actual image.
That's everything you need for now to get you linking images within CakePHP views. For more information regarding CakePHP and more tutorials please visit CakePHP.co.uk
