Most Important parts of web Design
When you take your business online, you are choosing to distribute your product line or services to the world. It is essential to make sure that your website is welcoming and gives visitors all of the information that they would require if they were in the market for your goods or services. more visit to :-www.instant-website-security.com If a person encounters a website that looks as if the information will be difficult to find or a site that is difficult to read due to the color scheme or various other reasons, that person will move on to another site containing the same or similar information. You will want potential customers to stay onyour website.
One of the is the layout of your page. The main page will be the page that most people will encounter first when visiting your website. This page should most importantly let people know what your company is and give a general explanation of what you have to offer the consumer. The main page will also contain links that will take visitors to other parts ofyour website that they will find relevant to the information they are seeking. Cluttering your main page with too much information is one of the most fatal flaws in web design.
Another important aspect of your webpage is making sure that it is easily found. Search engines have a process in which they frequently scan information all across the internet and collect information for their search results. This process is basically looking for what each webpage is about so that when internet users utilize the search, they will find exactly what they are looking for. Filling your webpage with content that is specifically designed to catch the attention of these search engines is extremely important.
A third aspect of web design that can make your webpage a success or a failure is ease of use. Many internet users want a site where they can find what they need quickly and without having to follow too many links. They want to easily see what you have to offer and be able to easily find your products, services or other information. If it takes a user too long to find what they want, they will most likely move on to another webpage that may be more user friendly than yours. more visite to :-www.instant-squeeze-page-mastery.com Drawing visitors to your website is only the first part of having a successful site. You want visitors to stay at your site and find what they are looking for.
Your webpage is your face to the world. Every person that visits your website will receive a first impression that will either cause them to look elsewhere or they will return to your site again and again.
Content management system
A Content Management System (CMS) is a collection of procedures used to manage work flow in a collaborative environment. These procedures can be manual or computer-based. The procedures are designed to:
* Allow for large number of people to contribute to and share stored data
* Control access to data, based on user roles. User roles are used to define each use as to what information they can view or edit
* Aid in easy storage and retrieval of data
* Reduce repetitive duplicate input
* Improve the ease of report writing
* Improve communication between users
In a CMS, data can be defined as almost anything – documents, movies, pictures, phone numbers, scientific data, etc. CMSs are frequently used for storing, controlling, revising, and publishing documentation. Content that is controlled is industry-specific. (Entertainment content differs from the design of a fighter jet). There are various terms for systems (related processes) that do this. Examples include: Web Content Management, Digital Asset Management, Digital Records Management, Electronic Content Management (and others). Synchronization of intermediate steps, and collation into a final product are common goals of each.
Contents
[hide]
* 1 Types of CMS
* 2 Enterprise content management systems
* 3 Web content management systems
* 4 See also
* 5 References
[edit] Types of CMS
There are six main categories of CMS, with their respective domains of use:
* Enterprise CMS (ECMS)
* Web CMS (WCMS)
* Document Management System (DMS)
* Mobile Content Management System
* Component content management system
* Media Content Management System
[edit] Enterprise content management systems
Main article: Enterprise content management
An Enterprise Content Management (ECM) system is concerned with content, documents, details, and records related to the organizational processes of an enterprise. The purpose is to manage the organization’s unstructured information content, with all its diversity of format and location.
[edit] Web content management systems
Main article: Web content management system
A ‘Web Content Management’ (WCM) system is a CMS designed to simplify the publication of Web content to Web sites, in particular, allowing content creators to submit content without requiring technical knowledge of HTML or the uploading of files.
Mastering Your WordPress Theme Hacks and Techniques
This is the first article in the four-part series, “Powerful guide to master Your WordPress”. Throughout this article, we’ll be focus on many WordPress Theme hacks, ideas, tips and useful tutorials you need to have ready in hand when developing WordPress websites.
We’ll keep this post updated during this month. As we’ll check your suggestions and feedback,
select the best, add them to this list and provide a direct link to the person who submitted
the suggestion.
So let’s get started and don’t forget to subscribe to our RSS-Feed to keep track on our next post in this series.
*
This is the Loop
1)The Loop is perhaps the most important thing one needs to understand when customizing WordPress theme. Any HTML or PHP code placed between where The Loop starts and where The Loop ends will be used for each post. When WordPress documentation states “This tag must be within The Loop”, such as for specific Template Tag or plugins, this is The Loop.
// the code inside the loop
o
Further Readings On The Loop
Wp9 in Mastering Your WordPress Theme Hacks and Techniques
1)Modifying Individual Posts In The Loop- Sometimes Clients request things like Google ads being displayed after post #2, the top post having a differently colored background, only displaying the title for a few posts. You can’t do any of this inside a single Loop, so here’s how to use multiple loops to display lists of posts differently.How to use multiple loops to display lists of posts differently.
2)Embed Google Ad in First WordPress Post- One of the more effective places to implement Google Adsense is after the first post, so you want to prevent your ad code from appearing after every single post, as WordPress would just loop and keep showing it. All you need to do is add this code inside the loop.
// Insert your Google AdSense code here
You can also publish the advertisement after the second post or the third etc. Just change:
“count == 1? to “count == 2?
*
Category Related Techniques
The first step in modifying what happens when someone visits a Category page is to figure out which of your theme’s files is going to be used to display the posts. This is known as the Template Hierarchy.
This diagram, tells us most of what we need to know about organization of WordPress themes.
Change the number of posts displayed in Category Page.
You will need to open index.php file and find the line below,
then add this line right below it:
Where number “4″ is the number of posts you want to be displayed on every category page.
Have a different template for specific Category
So, if you want to make the Category whose ID number is 7 look different from what it is currently (and different from other Category pages), just create a category-7.php template file (or whatever your category ID is) in your current theme folder and customize it as you please. WordPress automatically checks for this, and will use the file if it exists. Please notice how we added the “-” and after it the id number of the category.
Displaying single post pages differently in specific category
For example, if you have a category called “News” and a category called “Tutorials” and you want the Stylesheet of the “News” category to be style1.css, and the stylesheet of the “Tutorials” category to be style2.css. Lorelle provides a simple solution by following the directions below:
Open single.php file and delete all the content and add the following code
$post = $wp_query->post;
if ( in_category(‘9′) ) {
include(TEMPLATEPATH . ‘/single2.php’);
} else {
include(TEMPLATEPATH . ‘/single1.php’);
}
?>
In the most simple terms, the PHP code issues a query that says: Check the post. If the post is in category ID number 9, display single2.php. If not in category ID number 9, display single1.php.
o
Further Reading on Categories
1)What Every Blogger Needs to Know About Categories- You can help your users, improve your SEO, and gain absolute control over your content by implementing your own WordPress category solution!
*
Conditional Tags
Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches.
Styling different categories: If you want to use certain stylesheet for specific category, all you have to do is add the code below to the
tag in your index.php file.
o
Further Reading on Navigation Menu
Wp5 in Mastering Your WordPress Theme Hacks and Techniques
1)Creating Two-Tiered Conditional Navigation in Wordpress- Here is a common navigational scheme, with parent pages on top and child pages (if they exist) on bottom.
2) Smarter Navigation Menu Mike Cherim has explained how to get a smarter navigation menu using Conditional tags by offering post titles, archive dates, and category names instead of generic terminology.
HTML tag with tag.
o
Further Readings On Writing Code in your Posts
1)Signatures and Writing Code- Lorelle writes an amazing article on posting code in your WordPress.com blogs, listing the most common character codes (character entities) used in HTML/XHTML, PHP, and other programming languages. Also you need to check her other post “WordPress Plugins That Help You Write Code” sharing some of the WordPress Plugins that help you write code and equations in your blogs.
o 2)Online Code Converters- There are several online converters that transform the text you type into the standard HTML code that every browser can recognize without altering the character encoding to help you change code into something that WordPress blog can display properly.
+ Postable- An ajax online code converter created by “Elliot Swan”
+ HTMLizer Data
+ HTML Code converter
*
Organized and Structured Archives page
There are many techniques to customize your Archive Index page. Some involve incorporation of plugins or PHP code to create customized lists of archived posts, and others provide more interesting ways of displaying your archives. You can display Archives By Year, month, categories, day, Weighted Categories, etc…
The example below illustrats how to create an archives page listed by categories and months.
Where wp_list_cats displays a list of WordPress categories as links and wp_get_archives displays a date-based archives list.
If you want to retrieve every single page (perhaps for a custom archive page or a full article listing), you can set this to -1.
query_posts('posts_per_page=-1'); //to return all the posts without pagination
?>
o
Further Readings On WordPress Archives page
Wp11 in Mastering Your WordPress Theme Hacks and Techniques
1)How to Create An Archives Page- Here’s how to create an archives page for archive links listing by categories and months.
2) Creating an Archive Index
3)Wicked WordPress Archives in One Easy Step!- How to Create Archives with Real Sex Appeal
*
Permalinks
Setting your desired Permalinks structure is the first thing you should do after installing your WordPress blog because search engines index your blog based on post URLs and if you change from one to another then you will loose your rankings. You can change the Permalinks setting through Admin > Options > Permalinks.
WordPress has to option to use pretty links, turning the numbered links into sentences.
So instead of the default ugly permalink
http://example.com/?p=N
Use the Pretty Link structure
http://example.com/category_name/post-name
or
http://example.com/year/month/day/post-name
o
Further Readings On Permalinks Structure
1) Configuring WP Permalinks- Weblog Tools Collection has a useful guide on setting up permalinks on your WordPress blog and how to set up a .htaccess file if you don’t have one.
2) Understanding WordPress Permalinks- Which Permalink Structure Is the Best?
*
Identifying author(s) & guest comments and styling them
Through intelligent use of CSS, you can exercise absolute control over author and guest comment layout, presenting the reader with comments so rich with style.
1)How to highlight author comments in WordPress- By default, most themes have comments look the same. Matt Cutts show us a simple: instead of checking the author’s email address, check their user id to see if it’s the user id of the blog owner.
Wp2 in Mastering Your WordPress Theme Hacks and Techniques
2)How-to style WordPress author comments- Here you will be able to identify author(s) & guest comments under WordPress without the need for extraneous plugins, giving you some extra flexibility by adding code to check if the commenter’s email is the same as the email address of the blog’s author. 3) Styling Your WordPress Comments- Here are three examples to design the guest comments which complements the rest of the site using: Gravatars, Speech Bubble and Alternating Arrows.
Wp4 in Mastering Your WordPress Theme Hacks and Techniques
*
Separating Trackbacks from Comments
Trackbacks are the messages displayed in the comments list whenever another blog links back to one of your posts. It is best if they are not mixed with the comments.
Wp3 in Mastering Your WordPress Theme Hacks and Techniques
1) Separating Trackbacks from Comments- The method described here will lift out all of the trackbacks, and then display them as a numbered list after the list of comments is finished.
2) Managing Trackbacks and Pingbacks- This topic covers separating trackbacks/pingbacks from regular comments, and also how to remove trackbacks and pingbacks from a WordPress theme completely.
*
Custom 404 Page
Reconsider the value of your 404 page by thinking of it as your home page. Think of it as a gateway to your site, you will find good ideas here:
1)Customizing Your 404 Page- You can often retain the traffic that comes to your 404 page by either offering something funny to grab the readers attention or by offering a variety of methods for them to find the post they are looking for.
2)Customize Your 404 Page- Here are some tips for improving your 404 page on your personal (or even professional) website.
*
Gravatars
After you sign up for Gravatars service, every time you leave a comment at a blog with your valid email address, your image will appear next to your name, providing the owner has added a few simple lines of code.
How to Setup Gravatars for your Blog – Plug-in Free!- This is a guide custom tailored for WordPress users to setup gravatars on their blog without using a plug-in
*
Worth Checking Techniques
1) When Your WordPress Theme Keeps Reverting To Default- Have you ever been working on your WordPress blog’s design, when you refresh the page only to find that it’s decided to revert to the default theme?
Wp7 in Mastering Your WordPress Theme Hacks and Techniques 2) How to place a login form in the sidebar- Want to get your users log in through the front page, Small Potato’s tutorial will show you how.
3) When Your WordPress Theme Keeps Reverting To Default- Have you ever been working on your WordPress blog’s design, when you refresh the page only to find that it’s decided to revert to the default theme?
Wp12 in Mastering Your WordPress Theme Hacks and Techniques 4) Adding Recent Comments To Your WordPress Theme- Displays recent comments in the sidebar without the need for a plug-in: 5) Creating your home.php quite easily on WordPress- Two easy ways to use if you just want a static front page on your blog. 6) Even Simpler WordPress Contact Form- A simple solution for all your contact form needs. All comments left with this form are displayed in your WordPress administration panel. 7)WP Date Image Hack- Using dynamic images to replace the date entries of your blog post. 8 ) Category Page Hacks- These 2 hacks will increase the usability of WordPress category pages by giving an index of posts instead of a paged overview, and by sorting posts by title instead of post date. 9)Creating a “dynamic sticky”- When we need to have certain articles ’stay at the top’ longer than others. 10)Show Category Images- How to add some simple PHP code to your WordPress template and make linked images appear instead of text for your categories in posts. 11) Displaying WordPress Entries on static pages- A simple solution to include the date, headline, and excerpt of the latest entry from your Blog into your website static pages.
Wp1 in Mastering Your WordPress Theme Hacks and Techniques
12) Ajax Commenting- How to make your commenting system submit comment without reloading the whole page.
13) Opening Links in New Windows- This hack is for those who want to open links in a new browser window and don’t want to type out target=”_blank” every single time.
*
Developing a WordPress Theme
1)So you want to create WordPress themes huh?- A step by step guide to create a WordPress theme from scratch.
2) Developing a WordPress Theme- Dezinerfolio wrote a tutorial on how to develop a WordPress theme, you will learn to convert your xHTML CSS site into a Compact WordPress Theme. 3)How to create WordPress theme?- DevBox Wireframe WordPress theme, which is available for download on their site. This is basically a barebones theme that you can use to easily take a XHTML/CSS layout and convert it into a WordPress theme.
4)Widgetizing Themes- Technical instructions on updating a theme for use with widgets.
Enhanced Sidebars with Dynamic Content
After writing Effective Use of Blog Sidebars a couple of weeks ago I decided to look into the topic of dynamic sidebars. Typically, most blogs will us a standard sidebar for the entire site. WordPress users have a lot of potential functionality that is rarely used in terms of creating various sidebars and displaying them in different situations.
For example, when I was creating the theme for DesignM.ag I needed to have slightly different sidebars according to the category of the post. You can see the differences by looking at a standard blog post, a post in the community news category, and a post in the gallery category. For items in the news category I wanted a brief introductory paragraph with a button to submit news, followed by a list of recently published news items. On posts in the gallery I wanted the same thing, except for the gallery category instead of the news category. Posts in all other categories have a list of categories followed by recent news items. As a result there are three different left sidebars that are shown depending on the category of the post.
Why Would You Want Dynamic Sidebars?
Every blog visitor has different habits, but many tend to ignore sidebars because they are always the same. Mixing things up and keeping the contents of the sidebar relevant to the post can make them a bit more likely to be noticed. In short, blog sidebars are often ineffective because they are:
*
Boring
*
Outdated content (not changed frequently)
*
All the same throughout the blog
*
Not relevant to the rest of the page
*
Full of too many links with a cluttered appearance
What Can You Do to Fix Ineffective Sidebars?
Dynamic sidebars can improve just about all of the situations mentioned above. They’re not as boring because they change as the visitor moves through the site, they’re more relevant to the content of each page, and they won’t need to be as cluttered because only the relevant information is displayed on each page (as opposed to cramming everything into one generic sidebar).
Dynamic Sidebars in Action
Since I was writing a post about dynamic sidebars I figured I should put them into action on this blog as well. So far I’ve only made two changes to the standard sidebar, but more may be coming.
First, I added a small box of text in the right sidebar on posts in the Social Media category. I have a blog called Traffikd where I write primarily about social media marketing and blogging, so I decided to add a promotional statement for Traffikd on these posts since it’s relevant to most readers within that category (these are mostly older posts before I launched Traffikd). You can visit the post 50+ Designers to Follow On Twitter to see the change in action. The image below shows the block of text, it appears right after the banner ads and before the Popular Posts section in the sidebar.
Dynamic Sidebar
Second, based on the popularity of the collections of Photoshop tutorials, I decided to link them together in a different way. If you visit 40 Photoshop Tutorials for Lighting and Abstract Effects you’ll see a small list appear, again it’s in the right sidebar between the banners and the Popular Posts. You’ll see the headline “Photoshop Tutorials”, and then it links to all five of the collections of tutorials. You’ll also see a link for the post that you’re viewing because the 5 links are the same on all of the posts. It would be possible to set it up to only include links to the other four posts, but that would take some additional coding and I decided to keep it lighter.
It’s too early to tell if these minor changes have any noticeable effect in terms of click-throughs and enhanced navigation, but this just shows that there are plenty of options for creating your own dynamic sidebars.
Different Strategies for Creating Dynamic Sidebars
With WordPress you have some different options for achieving the end result of improved sidebars. Fortunately there are a number of good tutorials out there for these different techniques (plus the Codex), so I’ll try to point you in the right directions in addition to giving some of my own thoughts.
1. Use a conditional tag and a php include to insert an item.
This is the method that I used for both of the changes to the Vandelay sidebars. Using the first example, the php conditional statement essentially says, “if the post being shown is in the social media category, display the contents of the socialside.php file. If the post is in any other category, ignore this statement and just display the standard sidebar.”
This code is placed in the right sidebar php file exactly where I want the box of text to appear. The socialside.php file contains only one paragraph of text wrapped in paragraph tags and given a CSS class. The class of course is used to style the paragraph with the darker background and the border.
Here’s the code I’m using in my sidebar for this:
<?php if ( in_category(13) ) { include (’socialside.php’); } ?>
You can use this to do any number of different things with your sidebar if you get creative. Of course, you’ll want to change the number in the category to suit your needs. In my situation the social media category is number 13, but you may want to use another number, and you can exchange socialside.php for whatever you want to include.
In the second example that I’m using on this blog, I’m also using a conditional statement but in a different way. I wanted to display the Photoshop links only on 5 specific posts (probably more in the future when they are published). Unfortunately, these posts are not in their own category, they’re in the Design category, which has a ton of other posts where I don’t want these links to appear. This isn’t really a problem though, because you can also use conditional statements for specific posts instead of category. Each post has its own unique number, so it’s almost the same as the previous example.
Here’s the code I’m using:
<?php if ( is_single(324, 299, 279, 275, 242) ) { include (‘ps.php’); } ?>
The “is_single” tells WordPress to base the conditional statement on the number of the individual post being displayed. The three-digit numbers are the numbers of the specific posts, and then you have the include for the ps.php file, which simply contains the image for the headline and then the links. I can easily add another post to this statement if I was to publish another collection of tutorials, and I could add the new link in the ps.php file as well.
For more on conditional tags see:
* Conditional Tags – WordPress Codex
* The Ultimate Guide to WordPress Conditional Tags
2. Create separate sidebars for the front page, single posts, pages, archives, etc.
WordPress uses several different files (assuming your theme contains all of these files) based upon the part of the site/blog that is being viewed. The homepage layout is controlled by the index.php file. The layout of a single page is controlled by the single.php file. Layouts of pages (not posts) are controlled by page.php, and so on. Each of these files will likely contain the code to grab the contents of the sidebar. For example, mine uses:
<?php include (TEMPLATEPATH . ‘/l_sidebar.php’); ?>
If you want a different sidebar to appear on your frontpage as opposed to your individual posts, all you have to do is create a second sidebar file, l_sidebar2.php. Now, if you want that sidebar to appear on individual posts you can use this code in your single.php file:
<?php include (TEMPLATEPATH . ‘/l_sidebar2.php’); ?>
It’s a pretty simple change, but it gives you several different options.
3. Control the Style of the sidebar by switching the stylesheet
It’s also possible to use your stylesheet to give your sidebar different looks for various categories. It’s not an approach that I’ve used before, so I’ll point you to Using WordPress Categories to Style Posts from Lorelle via a guest author.
For lots of great information that can help with dynamic sidebars, see:
* WordPress Theme Hacks from Web Designer Wall.
* Customizing Your Sidebar from the Codex.
* Mastering Your WordPress Theme Hacks and Techniques from Noupe.
Some Ideas for Dynamic Sidebars
There’s really no limit to what you could do with dynamic sidebars. Here are a few of my ideas:
* Show author information
This wouldn’t require coding strategies listed above, but if you have multiple authors you could include the author bio in the sidebar to change things up a bit from post-to-post.
* Include a relevant ad or affiliate link
Maybe you have a specific post or category that would be great for promoting a specific affiliate product. You can use the strategy of conditional tags and includes to put it wherever you want.
* Make a series out of your posts
This is essentially what I’m doing with the Photoshop posts. You could adapt this strategy to fit your own content and funnel traffic through related posts on a specific topic.
* Create a call for subscriptions on high-traffic posts
Do you have a few specific posts that get a lot of search engine traffic consistently? Why not use a conditional tag and an include to put an extra call for subscriptions in your sidebar? I may try this approach myself.
PHP Tips and Tricks
PHP Tips and Tricks
These are some common PHP and MySQL coding tips.
I believe these are good do’s and don’ts, that most experienced coders would agree with, and not just my opinions.
Note: in my examples, I’m using the new arrays ($_GET/$_POST/$_COOKIE/$_SERVER) because I’m assuming that you’re using at least PHP 4.1.0. If you have an older version, then use the old, longer arrays ($HTTP_GET_VARS/$HTTP_POST_VARS/$HTTP_COOKIE_VARS/$HTTP_SERVER_VARS). But you should upgrade because of the file-upload vulnerability in unpatched versions prior to 4.1.2. That’s a tip by itself!
PHP
register_globals
First and foremost, I believe, is the use of register_globals. For those of you who don’t know, register_globals allows you to access variables from forms and URLs (such as file.php?var=foo) as $var in your script — “magically” created global variables.
Unfortunately, this old method continues to be used in most tutorials/examples. The PHP developers seem to have realized that register_globals was a bad idea, though. They discuss Using Register Globals in the PHP manual and recommend turning them off in php.ini:
Quote:
Note that register_globals is going to be deprecated (i.e., turned off by default) in the next version of PHP, because it often leads to security bugs.
…
You should do your best to write your scripts so that they do not require register_globals to be on
In fact, as of PHP 4.2.0, register_globals is now off by default on new PHP installations. By writing code that relies on register_globals being on, you risk having that code not work on some systems! Wouldn’t you rather use the preferred method of accessing variables and have your code work on all PHP installations?
The proper way to access these variables is via their respective arrays. So instead of $var, in the above example, you should use $_GET['var']. Similarly, use $_SERVER['HTTP_USER_AGENT'] instead of just $HTTP_USER_AGENT.
The main arrays are $_GET, $_POST, $_COOKIE, and $_SERVER, depending on where the variable came from, obviously. You can read more about them, and a couple of others, here and here in the manual.
Please access your variables via these arrays!
magic_quotes, addslashes(), and stripslashes()
magic_quotes_gpc, when on, automatically adds slashes to all GET/POST/COOKIE data so that you don’t need to use addslashes() before using GET/POST/COOKIE data in MySQL queries, etc. (e.g. with magic_quotes_gpc OR addslashes(), I’m becomes I\\’m). Well, magic_quotes_gpc is no convenience and just complicates things!
Since magic_quotes_gpc can be on or off, you don’t know whether to use addslashes() or not. You don’t want to use addslashes() when magic_quotes_gpc is on because you’ll add too many slashes (e.g. I’m becomes I\\\\’m), which is bad. Use addslashes() if magic_quotes_gpc is off, and don’t if it’s on (you can find out its setting with get_magic_quotes_gpc()). But you can’t use the same code all the time. One workaround is something such as:
if (!get_magic_quotes_gpc()) { $txt = addslashes($txt); }
Things are further complicated if you want to first manipulate text that has had magic_quotes_gpc applied. You then have some text that has slashes added and some that doesn’t. The effect of this is: some text will be wrong whether you use addslashes() or not.
It’s easiest to turn off magic_quotes_gpc, which I recommend, and use addslashes() manually all the time and not worry about the wrong amount of slashes. This is what it says in the recommended php.ini:
Quote:
magic_quotes_gpc = Off
Input data is no longer escaped with slashes so that it can be sent into SQL databases without further manipulation. Instead, you should use the function addslashes() on each input element you wish to send to a database.
As I said above, if you use addslashes() when magic_quotes_gpc is on, too many slashes will be added. For inserting I’m into MySQL, you want it to be I\\’m (and it will come out as I’m). Using addslashes() with magic_quotes_gpc, however, will give you I\\\\’m. THAT will come out of MySQL as I\\’m, which is not the original text. Most people assume that you are supposed to use stripslashes() when retrieving data from MySQL because otherwise they have slashes in their text. But that’s fixing a problem that should never have occurred. If you have to use stripslashes() on text from your database, it’s because you added too many slashes when you inserted it. You should never have to use stripslashes() on text from your database. If you do, you need to fix the problem at the source, rather than after the fact.
You can turn off magic_quotes_gpc in php.ini or like this in a .htaccess file:
If that’s not possible, you can put the following code at the top of all your files (in a require or include). It will strip the slashes that magic_quotes_gpc added, virtually turning it off.
function strip_magic_quotes($arr)
{
foreach ($arr as $k => $v)
{
if (is_array($v))
{ $arr[$k] = strip_magic_quotes($v); }
else
{ $arr[$k] = stripslashes($v); }
}
return $arr;
}
if (get_magic_quotes_gpc())
{
if (!empty($_GET)) { $_GET = strip_magic_quotes($_GET); }
if (!empty($_POST)) { $_POST = strip_magic_quotes($_POST); }
if (!empty($_COOKIE)) { $_COOKIE = strip_magic_quotes($_COOKIE); }
}
ereg vs preg
When it comes to the regular expression functions, ereg* and preg*, the preg functions are the clear choice. The preg functions are generally twice as fast as their ereg counterpart. They also support more advanced regular expression operations. I can’t think of any reason why you would need to use the ereg functions.
preg manual page and pattern syntax (long and confusing but pretty good).
PHP tags
I recommend always using the full PHP open tag, , it’s better to use the full . Using the full open tag ensures that your code will work on all PHP installations, regardless of the short_open_tag setting.
Alternative control structure syntax
I highly recommend staying away from the if (): … endif; style syntax and sticking with curly braces, if () { … }. This is the preferred syntax and it makes your code easier to read.
error_reporting
You should probably test your code with error_reporting set to at least E_ALL & ~E_NOTICE (all errors except notices) so that you aren’t suppressing errors that should be fixed. error_reporting can be set in php.ini or with the error_reporting() function.
Line breaks
People want to know how they can retain textarea line breaks in HTML. You should store text in the database in its original format (e.g. with just newlines) and then use nl2br() to convert newlines to HTML
tags on display. That’s all good, except for one problem with nl2br(): it doesn’t seem to convert \r newlines (edit: this has now been fixed in PHP 4.2.0).
Windows uses \r\n newlines; *nix uses \n; Mac uses \r.
nl2br() works correctly on text from Windows/*nix because they contain \n. However, if you get text from a Mac, nl2br() will not convert its newlines (again, fixed in PHP 4.2.0). To remedy this, I use the following bit of code to convert \r\n or \r to \n before inserting it into the database. It won’t hurt anything and ensures that nl2br() will work on the \n only newlines on display. Also, it has the side effect of saving 1 byte in the database per newline from Windows (by storing only \n instead of \r\n).
$txt = preg_replace(‘/\r\n|\r/’, “\n”, $txt);
Strings
1) As a finesse thing, I use single quotes around strings whenever possible (e.g. strings that don’t contain variables, single quotes, \n, etc.). This is supposed to make less work for the PHP parser.
2) When an array variable isn’t in a string, put quotes around string-literal keys so they are not regarded as constants:
// OK
echo $row[$key];
// Wrong, unless key is a constant
echo $row[key];
// Right
echo $row['key'];
// OK, since it’s in a string
echo “Text: $row[key]“;
3) Remember, you can break out of PHP mode for large sections of HTML. This is faster than echo’ing and you don’t need to escape quotes.
Quotes around numeric data in queries
For numeric columns in MySQL, you shouldn’t put quotes around any of their values in queries. As our resident database guru, MattR, says, “that is very non-standard and will only work on MySQL.” But if it’s unknown data, how do you know that it’s numeric and not letters that will cause an error? You can make sure that only a number is used in the query by first type-casting the data as int (or float for decimal numbers):
// If id is being passed in the URL
$id = (int) $_GET['id'];
$r = mysql_query(“SELECT * FROM table WHERE id=$id”);
Then even if id is set to “abc,” the worst that can happen is a 0 will be used in the query. No quotes; no error.
MySQL
Column types and attributes
1) Be familiar with MySQL’s column types along with their ranges/lengths. You should use the smallest column type that will hold the data that you expect to store. Most of the time, you probably don’t need INT, because MEDIUMINT, SMALLINT, or TINYINT have enough range. Using a smaller type saves space and speeds things up.
2) Declare all of your columns NOT NULL unless you need to store NULL values (NULL is not the same as 0 or the empty string). If you need to store NULL, you’ll know. Again, NOT NULL saves space and speeds things up.
3) Making INT-family columns UNSIGNED will effectively double the positive range with the same storage requirement. For example, TINYINT’s highest value is 127. TINYINT UNSIGNED’s highest value is 255.
Indexes and optimization
1) First, I sometimes see people create tables like this:
CREATE TABLE table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
…..
KEY (id),
UNIQUE (id),
PRIMARY KEY (id)
)
Do not make a column KEY, UNIQUE, and PRIMARY KEY. In this case, KEY and UNIQUE don’t help anything. The PRIMARY KEY is a key and it is unique. By specifying 3 indexes, it wastes space and slows down write operations.
2) Indexes/keys are the key (no pun intended) to fast queries (especially joins). The 3 types of indexes are PRIMARY, UNIQUE, and KEY/INDEX (same thing). It’s hard to explain when and how to use indexes, so I’ll just point you to some relevant pages in the MySQL manual: Speed of SELECT queries and How MySQL uses indexes.
3) When you have your indexes setup, use EXPLAIN on SELECT queries to make sure your indexes are actually being used.