Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Should I Omit (?>) PHP Closing Tag?

Friday, November 19, 2010
I never thought that the PHP closing tag was optional after years of working with PHP.  Before,  I consider it as a standard to have a closing tag just like the conditionals (if-else) and the loops (for, do-while, etc.) . . . you name it. Closure is enforced. Failing to do so results in a compile-time error.

A year ago, I became part of a project with a senior developer and he gave us a copy of coding standard that he wants.  The guideline was pretty impressive and I realized a lot of things which I can improved when it comes to coding.  Seeing that guideline convinced me that this man is really good. Then I started browsing his codes, then I get to notice that he isn't using the PHP closing tag on the controller and libraries we are using.  We are using Code Igniter. Boggled with this, I asked our boss why is he not using the closing tag at all. That was the time I realized that it is optional when the file is all PHP code and more importantly it prevents problems with the trailing white space.

PHP developers frequently encounter the problem with sending headers and cookies after output has been sent to the browser, but that problem can also happen with unintentional output. If whitespace gets inserted after the end of a PHP code block, that can produce unintentional output when that PHP script is included.

Zend Framework says that "For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response." Keep in mind that this idea / standard only applies to PHP files that are only for PHP - no HTML.

Kindly Bookmark and Share it:


".htaccess" Basics

Thursday, November 18, 2010
What is .htaccess?

Wikipedia says: In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. The .htaccess file is placed inside the web tree, and is able to override a subset of the server's global configuration; the extent of this subset is defined by the web server administrator. The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access the content), hence the name. Nowadays .htaccess can override many other configuration settings, mostly related to content control, e.g. content type and character set, CGI handlers, etc.

Where or when to use it?

Authorization, authentication
.htaccess files are often used to specify the security restrictions for the particular directory, hence the filename "access". The .htaccess file is often accompanied by a .htpasswd file which stores valid usernames and their passwords.
Rewriting URLs
Servers often use .htaccess to rewrite long, overly comprehensive URLs to shorter and more memorable ones.
Blocking
Use allow/deny to block users by IP address or domain. Also, use to block bad bots, rippers and referrers.
SSI
Enable server-side includes.
Directory listing
Control how the server will react when no specific web page is specified.
Customized error responses
Changing the page that is shown when a server-side error occurs, for example HTTP 404 Not Found.
MIME types
Instruct the server how to treat different varying file types.
Cache Control
.htaccess files allow a server to control caching by web browsers and proxies to reduce bandwidth usage, server load, and perceived lag.

Since .htaccess is a hidden system file please make sure your FTP client is configured to show hidden files. This is usually an option in the program's preferences/options.

How do I start?
1. Create an empty text file using a text editor such as notepad, and save it as htaccess.txt.
NOTE:The reason you should save the file as htaccess.txt is because many operating systems and FTP applications are unable to read or view .htaccess files by default. Once uploaded to the server you can rename the file to .htaccess.
2. Edit the contents of the file. Check the following examples:
  • Point an entire site to a different URL, such as domain.net redirected to domain.com:

    # This allows you to redirect your entire website to any other domain
    Redirect 301 / http://rv8820.blogspot.com/

  • Redirect index.html to a specific subfolder:

    # This allows you to redirect index.html to a specific subfolder
    Redirect /index.html http://rv8820.blogspot.com/newdirectory/

  • Redirect an old file to a new file path:

    # Redirect old file path to new file path
    Redirect /olddirectory/oldfile.html http://rv8820.blogspot.com/newdirectory/newfile.html

  • Redirect to a specific index page:

    # Provide Specific Index Page (Set the default handler)
    DirectoryIndex index.html

  • Redirect users to access the site without www:

    # To redirect all users to access the site WITH the www. prefix,
    # (http://example.com/... will be redirected to http://www.example.com/...)
    # adapt and uncomment the following:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.rv8820.blogspot.com\.com$ [NC]
    RewriteRule ^(.*)$ http://www.rv8820.blogspot.com/$1 [L,R=301]

  • Redirect viewers of your domain to use the secure version of your domain:

    # An easy to way to always redirect the user to secure connection (https://) can be accomplished with the following lines:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.rv8820.blogspot.com/$1 [R,L]

  • Redirect users to www for http:// and https://

    RewriteEngine On

    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{HTTP_HOST} !^www\.rv8820.blogspot\.net$ [NC]
    RewriteRule ^(.*)$ http://www.rv8820.blogspot.com/$1 [L,R=301]

    RewriteCond %{SERVER_PORT} 443
    RewriteCond %{HTTP_HOST} !^www\.rv8820.blogspot\.com$ [NC]
    RewriteRule ^(.*)$ https://www.rv8820.blogspot.com/$1 [L,R=301]

  • Redirect users to use https:// for a particular folder:

    # In case you wish to force HTTPS for a particular folder you can use the following:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{REQUEST_URI} somefolder
    RewriteRule ^(.*)$ https://www.rv8820.blogspot.com/somefolder/$1 [R,L]


3. Now upload the file to your root folder of your site and be sure to rename it to .htaccess without any filename extension.
4. Make sure that your redirect functions properly.
  • Paths to where you should save this file can be found in this article: System Paths
  • The definitive guide on Apache directives that can be used in .htaccess files can be found here: http://httpd.apache.org/docs/mod/core.html

Kindly Bookmark and Share it:


PHP Functions to JS

Friday, June 19, 2009

Have you ever encountered a problem in coding your Javascript and can only think using the PHP defined functions as a solution?

Well you're not the only web developer who have experienced that. As a growing programmer, I know I still have a lot to learn, and one of my weakness is the Javascript.

One time, while coding the JS script, I got puzzled on how should I check if the parameter submitted to my JS function is numeric or not. Hurried to finish the task, with no second thoughts I use the is_numeric(var) syntax inside an if statement. When I tried to check the page, it of course returns an error. I review the code and realized that is_numeric() is just one of the common functions I used when coding PHP.

Since, I've been with the same kind of situations a lot of times, I Googled for an application, script, plugins etc. which could help me convert php functions to JS functions and I luckily bumped into this site http://phpjs.org/.

So what is phpjs.org is all about?

PHP.JS is an open source project in which we try to port PHP functions to JavaScript. By including the PHP.JS library in your own projects, you can use your favorite PHP functions client-side.

Using PHP.JS may speed up development for PHP developers who are increasingly confronted with client-side technology.

It also offers added functionality because JavaScript does not natively support higher-level functions such as: md5(), strip_tags(), strtotime(), number_format(), wordwrap().

PHP.JS is nothing fancy like jQuery—they're just offering PHP functions, with all of their original flaws and benefits for whomever needs them.

Here is the PHP is_numeric equivalent function:

function is_numeric( mixed_var ) {
// Returns true if value is a number or a numeric string
//
// version: 904.317
// discuss at: http://phpjs.org/functions/is_numeric
// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +   improved by: David
// +   improved by: taith
// +   bugfixed by: Tim de Koning
// *     example 1: is_numeric(186.31);
// *     returns 1: true
// *     example 2: is_numeric('Kevin van Zonneveld');
// *     returns 2: false
// *     example 3: is_numeric('+186.31e2');
// *     returns 3: true
// *     example 4: is_numeric('');
// *     returns 4: false
if (mixed_var === '') {
    return false;
}

return !isNaN(mixed_var * 1);
}

You can download the full script at their site.

No server component required. To use PHP.JS you can either:

Kindly Bookmark and Share it:


How to Enable .htaccess on Apache

Friday, July 11, 2008

I was working on a site that needs to handle videos and uploading a video option is a must. One of my friend suggested to use VidiScript, so I downloaded a copy and hurriedly install it on my local server. The problem however is that I get Error 500 when I run the install scripts. I take a look on the scripts and discover that it uses .htaccess.

What is .htaccess anyway?

.htaccess files allows us to change configurations on our servers per directory or subdirectory. We may enable .htaccess files by editing our httpd.conf removing the comment on line from

;LoadModule rewrite_module modules/mod_rewrite.so

to

LoadModule rewrite_module modules/mod_rewrite.so

we need to change the AllowOverride directive also from

<directory>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</directory>

to

<directory>
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</directory>

You can also rename your .htaccess file by adding the line below on you httpd.conf file

AccessFileName [filename]

example: AccessFileName .configuration

Kindly Bookmark and Share it:


Mercury SMTP Server

Friday, April 4, 2008

Mercury MTS is a nifty free mail server from the same company that offers the free mail client Pegasus. Both can be downloaded from http://www.pmail.com/ .

Depending on the modules that you choose during setup, Mercury can act as a stand-alone SMTP server and client (to send and receive emails directly), SMTP relaying client (to send emails to Internet recipients through a mail server at your ISP), POP server (to deliver mail to local users) and POP client (to retrieve e-mails through your ISP's mail server, and deliver them locally through the Mercury POP server), mailing lists, filtering rules, aliases, support for black-listing spammers (ORBS, RBL), server-based email directory through PH, etc. Quite amazing for a free mail server!

XAMPP package includes Mercury and during installation the user is provided with the option to whether install the Mercury or not. Installing Mercury with XAMPP is as smooth as just clicking next button until the installation is finished.

I tried installing XAMPP on my local server and immediately test the PHP mail() function to test the mail server. The first error I encountered is this one:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\ramises\sendEmail.php on line 72

which made me realize that there isn't any mail server running on my local server. I opened the XAMPP control panel and made sure that Mecury is running. The XAMPP control panel has a "start" option on each of the modules. If Mercury is not running on your server, simply click "start" to make use of its service.

This time I am confident that the dummy script I made will run perfectly, but I have this problem now: Warning: mail() [function.mail]: SMTP server response: 553 We do not relay non-local mail, sorry. in C:\xampp\htdocs\ramises\sendEmail.php on line 72 If ever one of you meet "Server Response: 553", open the XAMPP control panel, click the admin button on the Mercury row to open the Mercury window. Go to "Configuration->Mercury SMTP Server->Connection control" and uncheck the "Do not permit SMTP relaying of non-local mail". Running my script again . . . . and whoaaalaaaaa, script runs perfectly!

Kindly Bookmark and Share it: