Archive | PHP Tutorials

PHP on Page Validation Form

You may have encounter website page types were should the user would make a error in filling the kind the error message would be shown (commonly in red) just previously mentioned the actual type command (area). I exhibit you the way to do this with PHP in this post.

Case in point HTML Kind
We shall make use of the subsequent HTML sort for illustration: Continue Reading

Posted in PHP TutorialsComments Off

Variables of PHP Scopes

Whenever you declare a variable outside a perform, it might not be found within the function. If you declare a variable within a perform, it may not be noticed outdoors the perform. That aspect is referred to as variable scope. I reveal the fundamentals of PHP variable scope during this post.

You require standard know-how in PHP so as to understand this sequence. In the event you don’t have that prerequisite understanding, then examine the series I wrote within this website titled,

Passing Arguments
Look at the adhering to code:

<?php

$ a = 4;

function sendValue($ x)
{
echo $ x;
}

sendValue($ a);

?>

Inside the over code, the variable, $ a is declared outside the operate. The definition on the functionality, basically sends the worth of its argument to the browser. If the purpose is termed, the variable, $ a is sent as argument. This worth is echoed. Now be aware two factors: This variable is declared exterior the perform. It is passed on the operate as an argument. Within the perform definition, the variable echoed is the parameter variable in the perform and never the variable declared outdoors the perform. Since the worth with the variable, declared outside the operate is passed as argument, with the definition of your perform, this value will become the value on the parameter variable.

Whenever a variable is declared exterior a function and passed as argument towards the purpose, the definition of the purpose sees the variable. The over code will work. Now, consider the adhering to code and note that it does not do the job:

<?php

$ a = 4;

function sendValue()
{
echo $ a;
}

sendValue();

?>

Right here, the variable remains to be declared outside the purpose. The operate does not have any parameter. When the operate is termed, the variable is not sent as an argument. On the other hand, with the functionality definition, the variable declared outside, instead of the parameter variable, is anticipated to become echoed. In a few laptop or computer languages, the previously mentioned code will perform. In PHP, it doesn’t perform mainly because a PHP functionality are not able to see a variable declared exterior its definition; that is just the rule of PHP.

World-wide and Regional Variables
In PHP, any variable declared
outside a operate as $ a over, is often a international variable. In PHP any variable declared inside a function (see below), is actually a nearby variable. With the adhering to code, the $ a declared outside the operate along with the $ a declared within the function are entirely two various points. Browse and try the adhering to code:

<?php

$ a = 4;

function sendValue()
{
$ a;
echo $ a;
}

echo “Value of variable outside<br “;
echo $ a; echo “<br “;

echo “Value of variable inside<br “;
sendValue();

?>

When you can see on the result, the 2 variables, nevertheless possessing the identical name, but by the reality that one particular is outside the operate along with the other is inside of, would hold diverse values. The 1 inside of the function within this situation, did not even acquire a value.

The reserved word, world wide
If you want the variable declared outside a function to maintain the same worth as being the a person within the perform, you’ve got to re-declare the one particular inside of the functionality, preceding it while using the reserved phrase, worldwide, as in the following code:

<?php

$ a = 4;

function sendValue()
{
global $ a;
echo $ a;
}

echo $ a; echo “<br “;
sendValue();

?>

Within the outcome, the two values displayed would be the similar. This illustrates the usage of the reserved term, global.

Superglobals
PHP has variables called super worldwide variables, or simply, Superglobals. They are variables that could be viewed outside and inside of capabilities without having the usage of the reserved word, world-wide. The programmer isn’t allowed to declare this sort of variables. You will find only many of them accessible and they are predefined. Examples of superglobals will be the $ _POST and $ _GET variables. They’re really arrays, not variables.

Note: Any time a world wide variable is passed as argument to a function, the operate definition will see the appeal in the variable; on the other hand, this doesn’t make the global variable, neighborhood or superglobal.

You will find other items to find out about variable scope, but to the goal of the tutorial, we shall stop right here. We continue with anything else within the next a part of the series.

Posted in PHP TutorialsComments Off

PHP Errors and Mistakes Tutorial

Programming Problems
There are actually a few different types of programming mistakes. Put simply, you’ll find 3 sorts of mistakes which will manifest in a very program. You’ve Syntax Problems, Logic Mistakes and Runtime Errors.

Syntax Errors
This is actually the wrong utilization of syntax. These mistakes are incorrect statements. When you sort a declaration, which happens to be mistaken, that is a syntax error. This sort of a declaration cannot be executed. One example is, inside a statement you’ll be able to kind a variable with out the $ signal. Under this problem, your application doesn’t get the job done. Determined by the way you configure your PHP installation, this kind of an error may well be indicated by PHP towards the output product just ahead of the system should be to be executed, whenever you give a command to run the software. By using a syntax error, the application just isn’t executed. Ahead of PHP code is executed, you can find some minimum amount compilation that requires location. Syntax problems could well be spotted because of the PHP compiler and reported, and execution (interpretation) in the system will not acquire spot.

Logic Problems
In this instance, PHP interpreter understands your plan quite properly and it executes the system. Even so, the software will never do that which you wished it to try and do. It’s going to do a little something slightly unique or absolutely distinct. The fault is yours. For instance, a loop that is necessary to do 10 iterations may do five iterations, since you coded it mistakenly to carry out 5 iterations. An additional example is usually that a loop could iterate infinitely, since the ailment you gave to the loop is wrong. Logic Problems occur when the system is staying executed (interpreted). The sole solution to solve this challenge is always to check your plan pretty very well just before you hand it on the customer (who asked for it).

Runtime Problems
Runtime problems manifest when the plan is getting executed consequently in the proven fact that you did not get sure factor into consideration when coding. For instance, let us say your code should be to divide eight by some denominator that the consumer inputs. If your person inputs two, the division will function, supplying you with 4 as response. In the event the person inputs zero, the division is not going to function, since 8/0 is undefined. Any time a runtime error occurs your method generally crashes (and prevent). To solve runtime problems, you will need to compose code that could reduce the execution with the certain code segment from taking place. During this division example, you must publish code that should avert division by zero from occurring, and possibly informing the user with the error he created by inputting zero being a denominator.

Avoiding Runtime Problems
Runtime mistakes are prevented applying what is identified as try-catch blocks. We shall make use of the case in point of dividing 8 by a denominator to illustrate this. Look at the following code:

<?php

$ input = 2;

try
{
if ($ input == 0)
throw new Exception(‘Division by zero is not allowed.’);
$ var = 8 / $ input;
echo $ var;
}
catch (Exception $ e)
{
echo “Error: “, $ e->getMessage();
}

?>

Try out the previously mentioned code. It is best to have 4 as the respond to, displayed. Now improve the value of the $ input variable at the beginning on the code to 0. You should have the text, “Error: Division by zero just isn’t permitted.” exhibited.

The code divides eight because of the value in the $ input variable. Once the price from the $ input variable is not zero, all is high-quality. Once the worth is zero, that may be an error, and therefore the program ought to not crash. Code has to be created to forestall the application from crashing.

You will find 4 important things you’ll want to notice in regards to the code above. There’s the try-block; there’s the catch-block; there’s an object established from the course named Exception; and there is certainly a declaration referred to as the toss assertion round the starting of the attempt block.

The try-block commences together with the reserved phrase, try out, then you definitely hold the pair of curly braces. The statements for that look at block are with the curly braces. The catch-block starts together with the reserved phrase, catch. It’s got parentheses by using a parameter. The parameter is really a variable, preceded by the variable variety (we have now not seen this variable style in advance of). The catch-block provides a pair of curly braces. The statements with the catch block go in to the curly braces.

The extremely first declaration with the try-block is definitely an if-statement blended with what is named the throw declaration? The thought is the fact that you check in the event the $ input variable is zero. If it truly is zero, then the throw declaration is executed to stop crash. If the throw statement is executed, we say an exception is thrown. When an exception is thrown, the statements below the throw declaration within the check out block are not executed; while the statements within the catch-block must be executed; that is certainly, when an error takes place, the statements down below the throw statement inside the try-block usually are not executed, even though the catch-block has to be executed to deal with the situation. If no error occurs (in this case, input is just not zero), then an exception will not be thrown. If an exception is not really thrown, the statements below the throw statements in the try-block are executed, plus the catch-block just isn’t executed.

The try-block has the typical statements to be executed to solve task required with the method. These statements are executed on ailment that no error has occurred. The catch block has the statements to get executed if an error happens. Often what the catch block does is the fact that it simply informs the person that an error has occurred by using a quick description on the error. If the error is detected in the look at block as well as catch block is executed as with the over code, then the method will not crash. I repeat, commonly, the catch-block would not do considerably over exhibit a short error message to your person. Prevention of crash is on account of the very fact which the normal statements with the check out block are usually not executed plus the catch-block is executed.

Introduction to the Exception Course
You can find a class termed the Exception class. This class has a great amount of members and solutions. For this simple tutorial we shall chat about only one of its members. The member retains the error information you need to give on the user. You, the programmer, would be the one who decides to the error information. You feed while in the error message after you create an object on the class. You build an object at the same time that you are throwing the exception. In the above code, we’ve,

throw new Exception(‘Division by zero isn’t permitted.’)

Here, the reserved phrase, toss, is usually to throw the exception. The exception is an object, which could possess the error concept and other important things. With this tutorial, we take into consideration only the error message. To produce an exception object, we commence while using the operator, new. This can be adopted, through the identify of the course, Exception; then parentheses. From the parentheses, you style the error message in estimates. The Exception course features a constructor approach that assigns this error concept as worth to 1 of its members (a single of its variables). This exception object is thrown to get caught be considered a catch-block. So the if-statement detects the error, throws an exception object, which is aware of the error as well as the catch-block catches the exception object. The catch-block employs information while in the exception object to complete the error managing.

The catch-block
The catch is like a functionality. It has parentheses, which has a parameter. The parameter will be the variable for an exception object. You decide on what ever title you wish for the variable, and precede it with the $ indicator. You precede the variable with the phrase, Exception. This implies the kind of variable is surely an exception. The throw-statement is like a get in touch with into the catch-block.

In the above code, we now have the echo declaration. The echo construct requires a comma-separated checklist of arguments. In this instance, the first argument may be the string “Error: “. This string are going to be displayed initial at the browser. This string suggests to the consumer that there’s an error. The description with the error follows through the next argument. This future argument into the echo assemble is really a return appeal of the system on the exception object. What now we have accurately is:

$ e->getMessage()

$ e is the catch parameter variable, which is the object caught that was thrown. getMessage() is the method to return the error message that we typed when creating the object. This is the only exception class method we consider in this tutorial.

Some Remarks
There is certainly an aged way of managing runtime errors in PHP; I will not examine that. There are also predefined exceptions; I will also not focus on that for this basic tutorial. What I have given you on this tutorial is plenty of to get you heading in PHP programming.

The NULL Price
This area is not truly handling problems, but is occasionally connected with problems. The reserved phrase, NULL, whose letters can be in any situation (higher or lower) could be assigned to a variable, like in,

$ someVar = null;

When a variable is declared devoid of any appeal assigned to it, the variable is regarded as NULL.

Posted in PHP TutorialsComments Off

Using PHP and HTML to create vibrant, dynamic web sites!

If you ever approach to create a online internet page with the close to foreseeable future, or potentially have a very contemporary internet page upward previously, you could be thinking about the most beneficial tactics to touch up up your Html web page expertise. Figuring out the way to code totally in HTML is mostly a terrific expertise to possess. The net incorporates a ton of other approaches to rule, various coming from HTML to be able to PHP & additional, more advanced languages, but realizing basic Html code has always been an incredible foundational ability to get. Although it’s possible you’ll do many of these website be employed in a WYSIWYG editor, you still require a solid foundation on the essentials of Programming to be a top-notch website design company. Even the ideal internet editing plans make mistakes that can require you to will end up in and make a few fixes by hand. If you’re serious about studying Html code you’ll find several incredible training & lessons available that can help you learn this specific skill. Continue Reading

Posted in PHP TutorialsComments Off

Why you might be a crap PHP programmer!

All of us have our poor routines. Within this write-up, we’ll go more than a checklist of poor practices which are really worth examining, reevaluating, and correcting instantly.

Who the Hell Do You’re thinking that You’re?

Each and every time I open up a undertaking that is not mine, it is accompanied by a tinge of worry that I am strolling into some sort of Temple of Doom situation, stuffed with lure doorways, key codes, and that 1 line of code that, on alteration, brings down the whole app (and probably sends a large boulder hurtling towards me down a slim hallway). Continue Reading

Posted in PHP News, PHP TutorialsComments Off

Hey! PHP Developers make your code secure!

PHP is arguably the most potent of all open-source programming languages. No longer used solely for web pages, it truly is turning out to be an more and more popular device for stand-alone applications and corporate purposes. In spite of all its electrical power and versatility, the PHP framework is far from protected. The a great number of amount of prosperous hacks on well-known world wide web purposes including Drupal, Joomla and WordPress serve as strong evidence. In this document, we are going to go through several of the most major protection difficulties to aid reinforce your shared, VPS or devoted internet hosting setting Continue Reading

Posted in PHP TutorialsComments Off

How to optimize and speed up your PHP script

This PHP tutorial will guide you into optimizing your PHP scripts to load and process quicker!

PHP has two different functions which are usually used to output data to a client – echo and print. Echo is faster than print by about eight percent.

Static is Better

For those who can declare one thing to be static, do it. A PHP script will be served at the least two to ten times slower by Apache than a static HTML page. So, attempt to use extra HTML pages and fewer server-side scripts. When you are working in OOP, if it could be a static technique, declare it static. This may increase your speed by a factor of 4.

Require() vs. Include()

require() and contain() are pretty considerably identical in each and every way, except that call for stops if the file is missing. However, as far as performance is concerned, there’s pretty little distinction. Also, whenever probable, use need() as an alternative to require_once(). Utilizing require is about a 3x to 4x speed-up over using require_once. Also, vital to know, require_once() is high priced.

Echoing Strings

If you echoing strings it’s basically faster to separate them by a comma as opposed to a dot. On the other hand, you need to know this only works with echo – which is actually a function which can take a number of string arguments. Also, make an effort to use echo’s multiple parameters rather than string concatenation.

Arrays and Single Character Functions

If a function – like a string replacement function – will accept both arrays and single characters as arguments, and if your argument list is not too lengthy, attempt writing redundant replacement statements, rather than a line of code that accepts arrays as search and replace arguments.

Stick to the best standards of coding

When you stick with coding standards it’ll make it a great deal less difficult for you to have an understanding of other people’s code, and in return they’ll essentially have the ability to recognize yours.

Just more Cache!

Try using memchached, a high-performance memory object caching program that speeds up dynamic web applications by alleviating the database load. Your PHP scripts are recompiled every time unless the scripts are cached. So, make certain to install a PHP caching item. This will increase your performance by 25-to-100% for the reason that it removes compile times.

Resources:

PHP tutorials for beginners

Posted in PHP TutorialsComments Off

A Guide to Linux coding with PHP- Introduction to Arrays

Introduction to Arrays

Since you will be astounding with variables, the subsequent finest point to suit your needs to find out are arrays. In contrast to variables, that retailer one particular worth, arrays let you retailer a lot of values within a variable – Clever things! Continue Reading

Posted in PHP TutorialsComments Off

The Basics of PHP Objects

When you have a set of variables and functions that get the job done collectively and would appear in lots of areas of your code, you’ll be able to put all that in one generalized unit identified as a class. There will probably be no require for repeat typing in the set. Here we’re chatting about a set of variables and capabilities. At the outset we talked about a set of statements that types a purpose. Here, we’re chatting about a set of variables and functions that form a class. The functions do the job along with the values from the variables. Below that problem, it’s feasible which the values of your variables and the outcomes from the accompanying capabilities could be transforming. So as a way to make use of the class, it’s important to build a specific unit within the class. That distinct unit is referred to as an object. Within this part of your collection, I give you the basic explanation of PHP lessons and objects. Continue Reading

Posted in PHP TutorialsComments Off

Secure your Clickbank products with PHP

Secure your Clickbank products with PHP

An effective method to protect the merchandise sold with Clickbank, utilizing the built-in protection and by applying a 30 day expiration, without having to manage databases or data lists of clients

Clickbank security is pretty good as it is. If you want to keep your clients from sending the thank you page link around to friends, there are some measures that can be taken.

Firstly Login to your Clickbank account: http://www.clickbank.com/login.html

Then click on the link to modify your clickbank account

You should arrive at the page where you can modify the pricing of your clickbank merchandise, scroll to the very bottom where it displays: Secret key (up to 16 letters & digits)

You will see a text box. If empty, select a secret key, type it in and dont forget it (not it down somewhere). It needs to differ from your clickbank password, but can be absolutely anything you desire.

Cookie Cutter Tools

If you have browsed around the clickbank website you will notice that clickbank has some scripts available in PHP that can help you secure your downloads. This is what generally happens:

1.) The order URL contains  a “seed”. Text within the url that can be simply anything you desire

2.) The visitor clicks on the order link and makes the purchase

3.) Clickbank takes this seed, and utilizes your secret key on it — simply combines the two together and generates some scrambled code. This scrambled code can only come from  both the seed and the secret key, any slight alteration to any of these would also change the scrambled code.

4.) Both the  seed and the scrambled code are sent back to the thank you page where your Clickbank script resides. This Clickbank script takes both the seed and secret key and scrambles the code exactly how clickbank itself does it

Clickbank calls this the cbpop (Clickbank Proof of Purchase.)

When the scrambled code match’s, then the visitor did make the purchase. The client cannot work out this out on their own since they never seen the secret key.
Its literally impossible for anybody to work out the right Proof of Purchase code without the secret key.

Using other code

This is the PHP function they give us:

function cbValid($ seed, $ cbpop, $ secret_key) {

// Code here..

}

This function cbValid takes 3 parameters: $ seed, $ cbpop, and $ secret_key. The script carries out the final step of ours mentioned above.
Ok, so now we need to work out what to do if your visitor didn’t pay. The best thing to do, is just halt the script immeadiatley, forbidding the page under it from processing.

if (!cbValid($ seed, $ cbpop, $ secret_key)) die();

The exclamation point means “not”. We’re saying, first try this…

cbValid($ seed, $ cbpop, $ secret_key)

.. pass the seed, proof of purchase, and secret key into the black box. If the function tells us NO, do the rest. In this case, “die”. Die stops everything immediately, so if you have HTML or PHP code below that line, it wouldn’t be looked at if the Clickbank validation fails.

The “proper” way to grab $ seed from the query string is this way:

if (!cbValid($ _GET["seed"], $ _GET["cbpop"], $ secret_key)) die();

You could also redirect the user to an error page of yours if you like:

if (!cbValid($ _GET["seed"], $ _GET["cbpop"], $ secret_key)) {

header(“Location:http://www.your.host/error.html”);

die();

}

Instead of $ seed and $ cbpop we use $ _GET["seed"] and $ _GET["cbpop"]. This is is due to variables that don’t appear, since they appear in the link as http://www.domain.url/test.php?seed=SOMESEED&cbpop=SOMEPOP. We want these values to be taken out of the URL.

Pre-Made Script

Here is a zip  containing the cb.php script: http://www.phpide.com/clickbank-script.zip

Download it, then unzip it, and then open cb.php. Find the following line:

$ secret_key = “YOUR_SECRET_KEY”;

Change YOUR_SECRET_KEY to the secret key you created in your clickbank account settings page.

Your thank you pages should end in .php here. Like, thankyou.php  it does not matter if they have apparent names – since they will be soundly unaccessible to unauthorized users. Note, you can basically rename your HTML pages so they end in .php and they will still work just as good

Put this line at the top of you thank you page script:

Make certain to upload cb.php to the same folder as your thank you page. When somebody goes to the thank you page, the 1st thing the thank you script will do is process everything in cb.php, and cb.php will accept the data Clickbank has authorized to see if it matches.

You are going to have to change your Clickbank order links a bit. This is what they should appear like:

http://www.clickbank.net/sell.cgi?link=YOUR_CLICKBANK_ID/YOUR_PRODUCT_ID/YOUR_PRODUCT_NAME&seed=YOUR_SEED

Replace YOUR_CLICKBANK_ID with, naturally, your Clickbank ID and YOUR_SEED with the seed you want to utilize. This can be anything, something basic that’s short and one word like the product name. But not your secret key.

YOUR_PRODUCT_ID is the number Clickbank shows to the left of each thank you page as you add it. When you’re trying this out, make sure to set the price at .00. Once everything’s in order you can increase the price of the item to .75 or .85 depending on the price of the product.

http://www.clickbankguide.com/merchant.htm#account will explain everything if you are new to clickbank

More prevention methods:

Preventing the visitor from sharing the downloaded file is impossible, but of course not the download link.. But there is a measure we can take to give unauthorized users a fair obstacle and that is utilizing the expiration.

We can say, 30 days after somebody buys your product, the thank you page will be unaccessible to them. If they purchase on May 15th, they can revisit the thank you page up until June 15th at the exact time they made their purchase. This provides enough time for the user to download the product but also makes it useless to share the link.

Creating the Expiration:

To work out the Unix timestamp of this very moment, subtracting 30 days is:

strtotime(“-30 days”)

Store this in a variable called $ expire:

$ expire = strtotime(“-30 days”);

The next question we may ask is, how do we know when these users bought the product? Remember, the seed you put in your order links can be anything you want. So let’s just make it the timestamp of this exact moment.

When the visitor revisits the thank you page, they cannot modify the seed, because as I mentioned, if you alter either the seed or the secret key, the resulting scrambled code (proof of purchase) will be slightly different. So you now see, they are stuck with it. However, the current time always changes.

Now we have to edit cb.php :

- Work out what the timestamp was exactly 30 days ago, and store this value in $ expire.

- Compare the seed and $ expire. If the the value of the seed is less than that of $ expire, it means that the product was purchased more than 30 days ago and the user should not be given access to the page. Die.

We have already taken care of step one by saving the timestamp 30 days prior in $ expire. Now, we compare the seed (it’s $ _GET["seed"], take note, since we are taking hold of of the url string) and $ expire like:

if ($ _GET["seed"] Order Now

Besides your seed we want PHP to call the function mktime(), that gives us the current timestamp, and output it, using echo.

echo mktime();

insert it in

“>Order Now

Assemble a link for .00 in your Clickbank control panel and try it. You can be certain it works by altering that “-30 days” in strtotime to “-1 minute”. Then try accessing the download page, then wait 1 minute and try again.

If you have multiple products, how do you keep somebody from seizing everything once they’ve took hold of one product?

Have your links look like the following: “>Order Now

This way the seeds will look like “dummys445433″ if you’re selling dummys. If you are selling toys, you can change “dummys” to “toys”. The seeds for each merchandise will vary.

The seeds won’t be all digits. So, in cb.php, do this:

$ timestamp = ereg_replace(“[^0-9]“,”",$ _GET["seed");

This tutorial wont go heavily into patcjing matching, but the [^0-9] means “NOT anything from 0 to 9. It simply goes through every letter and number of $ _GET["seed"], and if what is there Is not a 0, 1, 2,. It is substituted with nothing (thus the “”). The concluding result is saved down in a variable called $ timestamp.

Because now we are looking at $ timestamp and not $ _GET["seed"], let’s alter that if-statement:

if ($ timestamp

When I extracted the timestamp from the seed, just remove all characters that were not digits, leaving just the digits contained within that string. If we want to do the opposite. Here’s an example seed:

toy1074482258

We remove out all the digits and  left with “toy”. Next we work out which script called cb.php (that is stored in the variable $ _SERVER["SCRIPT_NAME"]). The script removes everything up to the last slash (/) and everything before the first dot (.). If the script was located at “/clickbank/toy.php”, all that’s left is “toy”.

If you name each thank you page a different name, and make certain all your seeds reflect the correct page, for example. if your thank you page is called “beans”, the part of that order link containing the seed should appear as:

&seed=beans

So that’s it, I hope this tutorial now helps to secure your Clickbank products!

Posted in Featured, PHP TutorialsComments Off