Pages

Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Sunday, February 27, 2011

Stripslashes () PHP Function

Definition: The function stripslashes is used to remove backslashes from data. One use for this function, is to display data to which addslashes has been applied. What this means is that stripslashes would change I\'m hungry into I'm hungry.
Also Known As: Strip Slashes, Remove Slashes
Examples:

 <?php
 $greeting = "Hello, I\'m Billy";
 print stripslashes($greeting);
 ?>

This would output the string: Hello, I'm Billy

 <?php
 $said = 'Who said \"Live long and prosper\"?';
 print stripslashes($said);
 ?>

This would output the string: Who said "Live long and prosper"?

Addslashes () PHP Function

 Definition: The addslashes function is used to add in backslashes [\] to data submitted from your forms. This keeps the input MySQL (and other coding languages) friendly. What this means is that addslashes would change I'm hungry into I\'m hungry.

It is phrased as: addslashes($string_data)
Also Known As: Add Slashes
Examples:

 <?php
 $greeting = "Hello, I'm Billy";
 print addslashes($greeting);
 ?>

This would output the string: Hello, I\'m Billy

 <?php
 $said = 'Who said "Live long and prosper"?';
 print addslashes($said);
 ?>

This would output the string: Who said \"Live long and prosper\"?

Make Addslashes More Universal

When writing user submitted data to a MySQL database, it is important to add the appropriate slashes to prevent errors. If magic quotes is running then there is no need to do anything, but if magic quotes is turned off then you need to run addslashes (). What if you want to make a more universal program, that will work for both types of PHP configuration?

One way to do it is to write a function that checks if magic quotes is running and then runs addslashes () based on the results. We check the status of magic quotes using the get_magic_quotes_gpc () function.

 <?php
 function Mod_addslashes ( $string )
 {
 if (get_magic_quotes_gpc()==1)
{
 return ( $string );
 }
 else
 {
 return ( addslashes ( $string ) );
 }
 }
 ?>

In the code above, we first check if magic quotes is turned on. If it is, we just return the data again. If it isn't we run it through addslashes () first. So, each place in our code where we would have normal run addslashes (), we will now run Mod_addslashes () instead.

Magic Quotes in PHP

What is Magic Quotes?: 
When turned on, Magic Quotes automatically performs an addslashes () on all form data submitted. This means that a [\] is placed before every ['], ["], [\], or nul in the data, so That's Great would be converted to That\'s Great automatically. This all happens before your coding even sees that data, so if you're just passing a string to the next page (and not to a database) it will print out with slashes even though you may not want them.

Why is it Good?: 
If Magic Quotes is running, you can be sure that you won't get any SQL errors due to illegal characters slipping by without a backslash. It saves having to run addslashes () on all the form data that's going to your database.

Why is it Bad?: 
You shouldn't learn to rely on Magic Quotes, or you won't learn to program without it. Also, when you let something automated run, your handing over control. It may seem easy now, but in the long run you may regret it. Plus, you may not always want slashes added, because not all forms go to MySQL, some just pass data to email or echo it on another page.

What about stripslashes (): 
Perhaps your asking, if Magic Quotes is running, why don't I just run stripslashes () on the data I don't want it on? You're right, this does work... BUT do you really want to make sure your running this on every script you ever run, and redo everything you already have running?

How do I know if it's on?: 
If when you run addslashes () you're getting three backslashes instead of one, it's probably running. You can check for sure in your phpinfo () or by running get_magic_quotes_gpc().

How do I turn it off?: 
If you have access to your php.ini file you can edit it to set magic_quotes_gpc = Off. You could also try doing it from your .htaccess by adding the line php_flag magic_quotes_gpc Off. Or when in doubt, contact your hosting provider.

Wednesday, September 15, 2010

Tutorial PHP & MySQL – Part 2: Your first scripts

OK. So now we should go to the folder where the PHP files are stored. If you’ve installed XAMPP to the standard directory, the web folder should be in C:/XAMPP/htdocs. Now make a folder with the name, for example, ‘php’. It’s time to make a first script to test the functionality of PHP.

Step 1: start Notepad++ (or any other text editor you want to use)
Make a standard HTML page, like the one below (it’s not necessary by the way):
<html>
<head>
<title>Test page</title>
</head>
<body>
</body>
</html>
Now, between <body> and </body>, put the code provided below:
<?php
echo “Hello world!”;
?>
A PHP script always starts with a ‘<?php’ tag and always ends with a ‘?>’ tag. The echo statement brings the text you typed between the double quotes to the screen. A line of PHP code mostly ends with a semicolon (;). Now let’s save our script and test it. Go to the folder ‘htdocs’ and save the file as, for example, test.php in the folder you’ve created (in my case the folder ‘php’). Now lets test it. In your browser type ‘http://localhost/php/test.php’. You should see a web page with ‘Hello world!’ on the screen now.

Let’s talk about variables now. This is very important in programming in PHP (and not only in PHP, in all programming- and scripting languages). Variables can store data, data collections and many more things for you to call it later on. Let’s explain it with a piece of code (make a new file or type this in your existing file):
<?php
$name = “Duco”;  //PHP variables always begin with a dollar sign.
$age = 19;   //Number variables (integers) don’t need double quotes, text (strings) do.
echo “Hello, my name is “.$name.”, and I’m “.$age.” years old”;  //I always use dots to seperate plain text from variables, it looks clearer in my opinion.
?>
If you try this in your XAMPP webserver, it will say “Hello, my name is Duco, and I’m 19 years old”. Now play with the values of the variables. Variables are very useful. If you, for example, have to write a date four times, you can store the date in a variable. And if you want to change the date, you only have to do it once.
There are four major types of variables you will use a lot in PHP:
- Integers: numbers (i.e. $number = 16;)
- Strings: text, it varies from one character to whole stories (i.e. $string = “Groningen”;, $string = “B”;)
- Decimal: an integer with a decimal value (i.e. $decimal = 15.41;)
- Boolean: a representation of ‘true’ (1) or ‘false’ (0) (i.e. $bool = true;, $bool = 0;)

Now let’s do some comparations. PHP has some comparison signs to check things.
<: smaller than
>: bigger than
<=: smaller than or equal to
>=: bigger than or equal to
==: equal to
!: not equal to   //for example, !< (not smaller than), != (not equal to)

Now for some code:
<?php
$age = 15;
if($age >= 18)    // If your age is equal to or bigger than 18…
{
echo “You can take driving lessons.”;
}
else
{
echo “You can’t take driving lessons, you’re too young.”;
}
?>
This script will say “You can’t take driving lessons, you’re too young.”. Now change $age to 18. It now will say you can take driving lessons.

You also saw the if-else construction. It’s necassery in any scripting- and programming language. If the conditions aren’t met, the actions written between the else brackets will be performed (by the way, you can’t use ‘else’ if you haven’t used an ‘if statement’ before. You can however use an’ if statement’ without an ‘else’). The open- and close brackets are necessary at each check.

Well, that was the second part of the tutorial. And if things aren’t clear, please say it, because this is my first tutorial.

Tutorial PHP & MySQL – Part 1: Setting up your environment

A must-have for a programmer is to have a piece of software on his computer to directly test his webapplication. You make a file, and you can directly test it in your browser. The other way to test a file is to upload it to your webspace and then test it. It’s way easier to do this on your own computer (and you don’t have the restrictions your webhosts charges you). I’m going to explain how to get this piece of software and how to install it.


The piece of software we’re going to install is XAMPP (This stands for X Apache MySQL PHP Pearl. The X stands for ‘many operating systems’. I’ve never used Pearl, so don’t ask me about it). This is a package which contains Apache, a program which serves the webpages for you and interprets PHP files as PHP files. Visitors can’t see the source code of your PHP-application, so it’s pretty safe. It also contains MySQL, an essential program for programmers who want an interactive website. It stores all your data (like login data), so you can access it later on. It also contains an e-mail package (I’m going to talk about this later, I haven’t figured out how this program works yet) and a FTP-package (which we aren’t going to use).

Step 1: download & install XAMPP
Go to http://www.apachefriends.org/en/xampp.html, and select XAMPP for Windows (or XAMPP for another OS if you don’t have Windows, but since I have Windows, I’ll pick this one). Under ‘Download’, pick XAMPP. Now pick the EXE- or ZIP-file.

Step 2: start XAMPP
Starting XAMPP is pretty easy. After you’ve installed XAMPP, go to Start Menu and start ‘XAMPP Control Panel’ from the folder ‘XAMPP for Windows’. Now you see a window with four start buttons. Push the start buttons at ‘Apache’ and ‘MySql’. If it’s OK, you should see two green boxes with ‘running’ in it next to the buttons. You could also check the ‘Svc’ checkboxes next to ‘Apache’ and ‘MySql’. This will install ‘Apache’ and ‘MySQL’ as services, so it starts with your operating system.

Step 3: trying XAMPP
Start your favourite internet browser. In the address bar, type ‘localhost’. This will bring you to the page of your local webserver (Apache in this case). Now you are on the standard XAMPP web page. Play around with it for a bit, it gives you an idea with what you can make with PHP and MySQL.

Step 4: install a nice text editor
To make and edit your PHP files, you will need some kind of text editor. You could use Windows Notepad, but it’s way too simple in my opionion. I always use Notepad++. It has the possibility to install plugins (like an explorer plugin). You can download the program here.

Well, this was the first part of the tutorial, and my very first tutorial ever written. If things aren’t clear, please make a comment.