PHP Programming 101

June 26th, 2015

Page Not Found!!!!

The annoying result of clicking on an image or link that presents nothing that can sway potential readers from continuing to explore a page. This would take a bit more reviewing of the code and other content but it ensures reliability and functionality enough to get your followers the information they need when they need it.

Sure you cannot anticipate or review them all but a periodic check should always be done to ensure this. On blogs, the short-links may have been erroneously saved when you changed the title of that article you saved as draft then published. Take time to place yourself in the footstep of the user and try out your page to isolate and fix such issues.

June 19th, 2015

History – a story of breadcrumbs

We all know the fairytale Hansel and Gretel where two kids use breadcrumbs they left along the way to get back home. That would greatly help users of your page to get a sense of control of where they are on your page. Most pages fail to do this preventing proper usage or not maximizing all the potential information your user can get from your page.

Control allows users to view through a simple “Home > About > Sub Page Title” line where they are at and should they want to get back to the second tier of the page, a simple click is all it takes rather than several on the browser history button.

June 12th, 2015

ステータスコードの数値

Posted by Conrad in Imp @ja


画像元:yourhtmlsource.com

応答ラインはリクエストは成功したかどうかを示ものだ。 これにはどの様なプロトコルが使われたか、ステータスコードの数値とステータスコードの短い説明が記載されている。

例として:

HTTP/1.1 200 OK

私達もステータスコードの数値をブラウザで時々見かける事がある。 理由は様々だ。 この数値は私達にとっては意味が無い、もしくは単に意味が分からないのだが、これらは何かしらの意味を持っている。

ステータスコードの数値は次の様な範囲に分かれる:

100-199
現在のプロセス・ステータスの情報メッセージ
200-299
リクエストの成功
300-399
ドキュメント又は資料が移動されたためのリクエスト中断
400-499
クライアントエラー。リクエストが不完全、不適切または解決不可能
500-599
サーバーエラー:リクエストは有効であるがサーバーがそれを完了できなかった

気付いただろうか?私達が目にする最も一般的なメッセージ「404 Not Found」エラーは、単に貴方がリクエストしたドキュメントは存在しないという意味なのだ。 これには二通りの考え方があり、本当に存在しない、もしくはURLを間違って入力したからかのどちらかだ。 404を返すとき、一般的にはブラウザ既定のフォーマットで表示される。 リクエストが不成功の場合、時にはサーバーがエラーメッセージと共にエラーレポートの詳細を送信することもある。

June 5th, 2015

Understanding Cookies

Posted by Edi in Information

Photobucket

What exactly is a cookie? It’s not a food, in the computer world, a cookie is a small amount of information sent by a Web server to a web browser. They are sent back unchanged by the browser each time it accesses that server. They are used for tracking, storing, authenticating information regarding the users which include sites visited. They give capabilities that make the web easier to navigate. If you are using Microsoft’s Internet Explorer to browse, you can see all of the cookies that are stored on your machine. They are usually stored in a directory called c:\windows\cookies.

It is composed of a cookie name and cookie value. The setcookie function() can be used to send a cookie to a browser to request your PHP page.

May 29th, 2015

PHP BASIC CODING (cont…)

(cont…)
III. PHP Variables

$variablename=value;

Variables are used for storing values such as text string or arrays. And all variables in PHP starts with a ($) dollar sign. There are various rules to be follow before naming a variable. 1) A variable name must start with a letter or an underscore. 2) A variable name can only contain alpha-numeric characters and underscores. Special characters are not allowed. 3) A variable name should not contain spaces.

IV.PHP Strings

$wrd=”Hello World”;
print $wrd;

Strings variables are used for values that contain character strings. The only operator that can be used on string is concatenation denoted by the sign (.) period.

$wrd1=”Hello World”;
$wrd2=”Good day”;
print $wrd1.”".$wrd2

You can count the nos. of string with the function strlen().

Print strlen(“HelloWorld and GoodDay”);

to be continued…

php-designer3php-editorscom.jpg

image source: www.php-editors.com

May 22nd, 2015

The Demand for Programmers

Posted by Hannah in Information

Programmer is one of the highest paid professions and as well as one of those that remain in demand. The reason behind this is simple. As long as the Internet and the online community as a whole remain progressive and needed and so is the need for programmers remain because behind the success of the online world are programmers that work toward making every aspect of it possible, visible, and beneficial to every user. Programmers are responsible for social networking sites such as the most popular Facebook. They are as well the ones that invent billions of websites that users benefit from until today. There are more to these contributions that make programmers even more needed. As long as those social networking sites, websites, blogs, and the totality of the web are being used as source of information and entertainment, the demand for programmers will always be inevitable in a society and with this; more and more people will strive to be one.

May 15th, 2015

PHP PROGRAMMING 101: GETTING TO KNOW WHAT IS PHP?

Posted by binary in PHP

What is PHP?

PHP is originally known as Personal Homepage but now it’s already known as Hypertext Preprocessor. This general-purpose scripting language was created by Rasmus Lerdorf in 1995. And its main use is to produce Dynamic Web Pages.

PHP is one of the widely-used scripting languages over the web servers because it’s free of charge. PHP can also be used for command-line scripting and client-side GUI application. And in terms security, PHP is the popular target of hacker especially those application that is poorly built.

PHP focuses on server-side scripting. And it’s like other server-side scripting languages that provide dynamic content from a web server to a client such as Microsoft ASP.NET, Sun Microsystem JavaServer Pages and mod_perl.

According to wikipedia.com “PHP is the fourth to the most popular computer programming language, ranking behind JAVA, C, and Visual Basic. PHP was also recorded as the most popular Apache module. And as of April 2007, 20 million Internet domains were hosted on servers with PHP installed.”

php

Image Source: www.filebuzz.com

May 8th, 2015

The numeric status code

Posted by Conrad in Codes


Image Source:yourhtmlsource.com

A reply line indicates whether a request was successful. It includes the protocol being used, a numeric status code, and a short description of the status code.

An example would be:

HTTP/1.1 200 OK

We often see numeric status codes in the browser every now and then. For so many reasons. These numbers may look jumbled to us or we simply cannot understand them but it stands for something.

The numeric status codes fall into the following ranges:

100-199
Information messages on the current status of processing.
200-299
Successful request.
300-399
Request cancelled because document or resource has been moved.
400-499
Client error. The request was incomplete, incorrect, or otherwise unresolvable.
500-599
Server error. Request appears valid, but server could not complete it.

Have you observed? The most common status message we get to see is the “404 Not Found” error, it just means that the document you requested does not exist. Two things though, this is either because it really doesn’t exist or because you entered the URL wrong. When a 404 is returned it is usually displayed on the browser screen in whatever default format is used by that browser. The server may also transmit a detailed error report page along with an error message if the resource call was unsuccessful.

May 1st, 2015

Random Numbers

Posted by Conrad in Basic Programming, PHP, Programming


int rand ( [int min, int max])
int mt_rand ( [int min, int max])
int getrandmax ( )
int mt_getrandmax ( )
void srand ( [int seed])
void mt_srand ( [int seed])

Sometimes you want to take random actions in your code – it might be to give your web site visitors a different greeting each time they visit, you might be programming a game, or you might be trying to secure data by hashing it. Either way, randomisation is simple and helpful thing to remember, and has just two functions: rand(), and mt_rand().

Both functions do the same thing, and both take the same parameters, so what is the difference between the two? Well, rand() is a basic randomisation function that is very quick but not very “random” – the numbers it generates are slightly more predictable. Mt_rand() on the other hand, is more complicated – the “mt” parts means Mersenne Twister, as that is the name of the randomisation algorithm it uses. Mt_rand() returns much more “random” numbers, but does so at the expense of some speed.

As mentioned, both functions have the same parameters – two optional numbers, for the minimum number to return and the maximum number to return. Either you supply no parameters, which will result in PHP returning a random number between one and a very high number, or you can supply the two parameters. Here is an example:

$random = rand();
$randrange = rand(1,10);
$mtrandrange = mt_rand(1,100);
?>

Note that the two numbers passed in are inclusive. That is, our $randrange number could be anywhere between 1 and 10 including 1 and 10.

As mentioned, if you do not pass any parameters to your rand() and mt_rand() calls, PHP will generate a random number from 1 to a high number. If you want to find out the maximum number PHP can return from a rand() call, use getrandmax(). There is a similar function, mt_getrandmax() for mt_rand().

Now you know how randomisation works, here is a quick example to show you how you can make use of randomisation to greet web site visitors in various ways:


switch(rand(1,6)) {
case 1:
$greet = 'Hello!'; break;
case 2:
$greet = 'Welcome!'; break;
case 3:
$greet = 'Greetings!'; break;
case 4:
$greet = 'Salutations!'; break;
case 5:
$greet = 'Good day!'; break;
case 6:
$greet = 'Yo!'; break;
}

print $greet;
?>

Here we have not bothered assigning the result of rand() to a variable before putting it into the switch statement, but you can do it whichever way is easier for you to read.

One important thing to note is that the speed of randomisation does not depend on the sizes you pass into it – rand() is just as fast in rand(1,3) as it is in rand(1, 10000000). Mt_rand() works just short of 50% slower than rand(), which means you should only be using it if you particularly need the extra randomisation it brings.

To give you an idea of how fast the two run and how using larger values for randomisation makes no difference, try this script:


$START = time();
for ($i = 1; $i < 1000000; ++$i) {
$j = rand(1,100);
}
$END = time() - $START;
print "Short rand() took $END seconds\n";

$START = time();
for ($i = 1; $i < 1000000; ++$i) {
$j = mt_rand(1,100);
}
$END = time() - $START;
print "Short mt_rand() took $END seconds\n";

$START = time();
for ($i = 1; $i < 1000000; ++$i) {
$j = rand(1,10000000);
}
$END = time() - $START;
print "Long rand() took $END seconds\n";

$START = time();
for ($i = 1; $i < 1000000; ++$i) {
$j = mt_rand(1,10000000);
}
$END = time() - $START;
print "Long mt_rand() took $END seconds\n";
?>

Most random number generators require “seeding” – initialising with a starting value – because the numbers they generate are not truly random. Instead, they are known as pseudo-random, meaning that they appear to be random. The seed value is used to generate the first number, the first number is used to generate the second number, the second for the third, etc, meaning that if you always supply the same seed value you will always get the same string of “random” numbers. This is actually advantageous. Many years ago there was a popular game called Elite available on the BBC Micro, where the player was allowed to fly around a large universe of eight galaxies, each with thousands of star systems. Each star system had a very precise number of planets, a distinct economy situation, etc, and yet the entire universe fit into just 22K of memory. How was this possible? Simple: by providing the same seed to their random number generator, the exact same universe could be generated each time.

Of course, this is a fairly rare situation. More often than not you will want numbers that look random as opposed to numbers that are always the same, and this is where random seeding comes in. If you provide a random number to the random number generator as its seed, you will have a new and original string of random numbers coming out. Does this sound like a chicken and egg situation to you? That is, how do we get the random number to provide to the random number generator? Well, think of what randomness – usually called entropy – you can draw upon in your scripts.

  • The number of files in your temp directory?
  • The number of rows in your database?
  • The time your script was called?

Of all three of these, the latter is potentially the most random – you do not control when your script is called, and you are certainly never likely to have the same script called in the exact same microsecond, so you could use the return value from microtime() as your initial random seed.

The seed function for rand() is srand(), and it takes one parameter – an integer to use as the seed value. If you are using mt_rand(), you should use mt_srand() for seeding. If you recall from earlier, microtime() returns a floating-point number – this is no good for use as the parameter to srand() (or mt_srand() – it is exactly the same), so you need to make it into an integer before use.

Now, as we know that microtime() returns the time in millionths of a second, we can convert its return value to an integer by multiplying it by a million, like this:

srand((double)microtime()*1000000);

The code above should seed the standard random number generator fairly well. You can do the same for the Mersenne Twister generator with this code:

mt_srand((double)microtime()*1000000 );

If you want your random number to always generate the same string of numbers, simply supply a seed that is a known value. For example, no matter how often you run it, this next script will always generate the same “random” numbers:


mt_srand(123456);
echo mt_rand(1, 100), "\n";
echo mt_rand(1, 100), "\n";
echo mt_rand(1, 100), "\n";
?>

The last option is just to call srand()/mt_srand() without any parameters at all. In this situation, PHP will attempt to generate a random seed for you – not much good if you want reliably random numbers or if you have a particularly good source of entropy for your seed value, but generally good enough for most people.

As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is done automatically.

Source

April 24th, 2015

PHP Browser Checking Script

Posted by Conrad in Basic Programming, Information, PHP

Several factors are to be considered should one be involved with deploying projects over the internet. One of the things you could do would be to have a nifty script that checks for the particular browser your clients might be using. This can be done with a simple and short piece of code that can mean the difference between a successful deployment without problems allowing proper extensions and plugins to be included. A sample of the code can be seen below for your reference which makes it easier for your program/web page to know which set of scripts and what parameters to adhere with. (more…)

Next Page »