Nov 18, 2011

PHP EXAMPLES

Mail or Email Example in PHP with HTML

When requested to send a mail or email from our website, we went to a programming language, with the function mail () PHP can send email without having an email client installed on an apache server or anyone who supports this function Its syntax is very simple:

[Code]
mail ($ to, $ subject, $ body); [/ code] 

If you deal in this way we come plain, or only text, but what happens when we send mail with formatting, tables or images.

For this we will support the final parameter of this function is where can we send headers and indicate that it is an html file for processing as such.

And just add the following line to have our email in HTML format and code.

[Code]
Content-type: text / html [/ code] 

At the end of our role will be as follows:

[Code]
mail ($ to, $ subject, $ body, 'Content-type: text / html');[/ code] 

This would be an example of PHP mail in HTML format:

[Code]
<? Php
$ Destination = "contacto@miserver.com";
$ Subject = "Web Contact";
$ Headers = "Content-type: text / html";
$ Body = "Hello, someone has contacted the Web form on your site <br>
The data sent are as follows: <br>
<b> Name: </ b> $ name <br>
<b> email: </ b> $ email <br>
And submit the following comments: <hr>
<pre>
$ Comment
</ Pre> ";
mail ($ destination, $ subject, $ body, $ headers);
echo "Message has been sent successfully";
?>
[/ Code]


Example PHP Function

In this example you will see how they work in PHP functions.Pretty simple. The functions will be to take the square and the cube of the number that we pass as a parameter to the function.
[Code] <?
# Example of EjemploDe.Com
calcular_Cuadrado function ($ number) {
$ Calculation = $ number * $ number;
return $ calculations;
}
calcular_Cubo function ($ number) {
$ Calculation = $ number * $ number * $ number;
return $ calculations;
}
echo "Hello <br/>";
$ Root = calcular_Cuadrado (20);
echo "The square root of 20 is:". $ root. ". <br/>";
echo "and the cube root of 20 is:". calcular_Cubo (20). "<br/>";?> [/ Code]
As shown in the example, we create two functions, and calcular_Cubo calcular_Cuadrado, and then the show, we can both keep it in a variable, or simply show an echo THROUGH.



Password Generator Example

The following code will generate a password in PHP, ranging between 5 and 12 characters, you can also change.
Is useful when you make a user system, the part where a user forgets his password, so you can generate a password and send it to your mail and store it in the database, if you have encrypted passwords.
[Code]
<?
# For EjemploDe.Com
generarPassword function ($ characters) {
$ Password = "";
$ Characters = "0123456789bcdfghjkmnpqrstvwxyz !"#$%&/()=?,;.:-_}{*][*-+/";
$ I = 0;
$ While ($ i <$ characters) {
$ Char = substr ($ characters, mt_rand (0, strlen ($ characters) -1), 1)
if (strstr ($ password, $ char)) {
$ Password .= $ char
$ I + +;
}
}
return password;
}
$ Cantidadchar = rand (5, 12);
$ Password = generarPassword ($ cantidadchar);
echo "The generated password is:". $ password. ""
echo "Courtesy of <a href="http://www.ejemplode.com"> <br/> EjemploDe.Com </ a>";
?>
[/ Code]


Example of Read and display text file in PHP

In this example we will see how to display a text file in PHP.
We have two forms, the method include, and the other reading the file, using file or file_get_contents.
The method is to include usually include text, and with the other method can save the contents of text in a variable and be able to format or replace other words, put in all uppercase letters, etc..
We need a file.txt, we may use another name, just change the parameter in functions
1: Example with include
[Code] <? Php
# Ejemplode.com
echo "text file file.txt <strong>: </ strong>";
include ("file.txt");
?> [/ Code]As you can see, it prints the text as it stands can not be formatted, browser may not make the line breaks, because in the txt <br> not used, but using a common line break, we can use <br> in the. txt, or else use the second example, which will allow us to format, and we can make a nl2br2: Example with file_get_contents[Code] <? Php
# Ejemplode.com
$ File = file_get_contents ("file.txt"); / / Save $ file file.txt
$ File = ucfirst ($ file) / / We give some form
$ File = nl2br ($ file) / / Converts all line breaks in tag <br/>
echo "text file file.txt <strong>: </ strong>";
echo $ file;
?> [/ Code]



Example of Read and display text file in PHP

In this example we will see how to display a text file in PHP.
We have two forms, the method include, and the other reading the file, using file or file_get_contents.
The method is to include usually include text, and with the other method can save the contents of text in a variable and be able to format or replace other words, put in all uppercase letters, etc..
We need a file.txt, we may use another name, just change the parameter in functions
1: Example with include
[Code] <? Php
# Ejemplode.com
echo "text file file.txt <strong>: </ strong>";
include ("file.txt");
?> [/ Code]
As you can see, it prints the text as it stands can not be formatted, browser may not make the line breaks, because in the txt <br> not used, but using a common line break, we can use <br> in the. txt, or else use the second example, which will allow us to format, and we can make a nl2br
2: Example with file_get_contents
[Code] <? Php
# Ejemplode.com
$ File = file_get_contents ("file.txt"); / / Save $ file file.txt
$ File = ucfirst ($ file) / / We give some form
$ File = nl2br ($ file) / / Converts all line breaks in tag <br/>
echo "text file file.txt <strong>: </ strong>";
echo $ file;
?> [/ Code]

Example of random numbers and letters

This code in PHP, will generate a random number and aletter. It's great for giveaways, or do random things.
It uses the function rand (); for numbers, and chr (), or ord (), to show a letter from a random number according to the ASCII table.
You can set the variables to adjust the range of generation of both letter and number.
The variables are set to generate letters from the D to the Y.And the number generated would be between 1 and 1000
[Code]
<?
# Example to generate and display number and letter
# Courtesy of EjemploDe.com
/ / Variables
$ DesdeLetra = "d";
$ HastaLetra = "y";
$ DesdeNumero = 1;
$ HastaNumero = 1000;
$ LetraAleatoria = chr (rand (ord ($ DesdeLetra), ord ($ HastaLetra)));
$ NumeroAleatorio = rand ($ DesdeNumero, $ HastaNumero);
echo "<strong> random letter generated is: </ strong>". $ letraAleatoria. "<br/>";
echo "<strong> random number generated is: </ strong>". $ letraAleatoria. "<br/>";
echo "Example taken from <a <br/> href="http://www.ejemplode.com"> EjemploDe.Com </ a>";
?>
[/ Code]

Example of Uploading Files

This example is a file upload form using the type FILE HTMLforms then are sent via POST to the server and so is done rising
Too simple, without checks or anything. Remember to have a folder called uploads and write permissions to the public
[Code]
<?
if ($ _POST) {
$ File = $ _FILES ['file'] ['name'];
$ EndAddr = "uploads /". $ File. ""
if (is_uploaded_file ($ _FILES ['file'] ['tmp_name'])) {
copy ($ _FILES ['file'] ['tmp_name'], $ EndAddr);
}
}
?>
<Form action ="<?=$ PHP_SELF?> "Method =" post "enctype =" multipart / form-data "name =" form1 ">
<input type="file" name="archivo" id="archivo"> ​​<br />
<input type="submit" name="button" id="button">
</ Form>
[/ Code]


Sessions in PHP Example

Example use of sessions in PHP. In this example, we will see how to create sessions, and can use them in other pages apart, until the user session ends, usually when you close the browser.
File: acceso.php
[Code] <?
session_start ();
$ _SESSION [Access] = true;
echo "OK";
?>
[/ Code]File: verificar.php
[Code] <?
session_start ();
if ($ _SESSION [access] == true) {
echo "OK, you have allowed access";
}
else {
echo "Error, you have permission."
}
?>
[/ Code]

In this simple example we have divided into two files, we see that in acceso.php is the file that we create a session called access, and opening the page we create the session, with a value of true or real, then displays OK. And then open up and show us verify that we have allowed access, as we open acceso.php. If we have not opened the acceso.php, we will fail the verificar.php, since we have created the session that creates the acceso.php. Really simple to understand and easy to implement for systems in which users do not want to use cookies and prefer to use sessions for safety.

Nov 17, 2011

PHP EXAMPLES

Sample Date and Time in PHP

To print the date and time on screen use the PHP date () function. We can handle the format to display by passing it as the first argument

Here is an example
It is too simple, and can handle the format as eg. Day / Month / Year. O Day-Month-Year, the type of numbers, for example, January = 1, January = 01. Show the month name directly.Even though he would need to translate it. Since the language is generally used on the server


[Code] <html>
<body>
The date today is: <? = Date ("d / m / Y");?>.
<br />
The server's local time is: <? = Date ("G: i: s");?>.
</ Body>
</ Html> [/ code]







Read Sample contents of a file

In this example we will see how to save to a variable content of any other file and display it.

[Code] <? Php
$ Text = file_get_contents ("texto.txt") / / read and store the file in $ text texto.txt
$ Text = nl2br ($ text); / / replace with a new $ text $ text, but change the line breaks () for a line break in html (br)
echo $ text; / / show the text
?>
[/ Code]
The truth is quite simple, and using file_get_contents, we could read different types of files and process them in different ways ...






Sample text replacement by another



A simple example of how to replace a text by another in PHP ...

[Code]
<? Php
$ Text = "I have two sweets. Also I have three lollipops, but I want to eat the two candies. And I would buy two gum";
$ Variable = str_replace ("two", "four", $ text);
echo $ variable;
# The end result would be: "I have four candy. Also I have three lollipops, but I want to eat four candy. And I would buy four gum."
?>
[/ Code]



Back Button Example in PHP

Many times we need to put a button to return to where they were our customers, there are different ways to make the example presented below is written in PHP:
<? Php Echo '<a href="'.$_SERVER['HTTP_REFERER'].'">Back Button Example in PHP </ a>';
?>
This will take a page to which we come.

While Loop Example in PHP

The PHP while loop is used to run a piece of code while the condition remains true. Generally counters are typically used for, or to display records from a database.
In this example, the loop ends when the counter reaches 10.We will do this using a variable called $ i, which is usually identified as an accountant

[Code]
<?
# EjemploDe.COM
# While Loop Example in PHP
$ I = 0; / / Inializamos the variable to 0
while ($ i == 10) {
$ I + +; / / increase $ i by one
echo "i equals now." $ i. "</ br>" / / show text
}
echo "End of the while loop. The while loop ends in". $ i. "";
?>
[/ Code]This example displays the following[Code]
i now equals 1
i now equals 2
i now equals 3
i now equals 4
i now equals 5
i now equals 6
i now equals 7
i now equals 8
i now equals 9
i now equals 10
End of while loop. The while loop completed in 10
[/ Code]


 Example of Calculations in PHP


It works very simple, just set the two variables, $ $ SegundoValor PrimerValor and now are 10 and 30.
<?php # Comentarios. # Ejemplo de programacion en PHP simple # Haciendo simples calculos entre variables predefinidas # Por EjemploDe.Com 
$PrimerValor = 10; //Comentario: Edita el numero para cambiar el primer valor $SegundoValor = 30; //Comentario: Edita el numero para cambiar el segundo valor 
//Seccion de calculos $suma = $PrimerValor + $SegundoValor; //Sumamos $resta = $PrimerValor - $SegundoValor; //Restamos $multiplicacion = $PrimerValor * $SegundoValor; //Multiplicamos $division = $PrimerValor / $SegundoValor; //Dividimos 
//A continuacion mostramos las cuentas en un formato textual 
echo "<h1>Formato Textual</h1>"; echo "La <strong>suma</strong> entre <strong>".$PrimerValor."</strong> y <?php
# Comentarios.
# Ejemplo de programacion en PHP simple
# Haciendo simples calculos entre variables predefinidas
# Por EjemploDe.Com

$PrimerValor = 10; //Comentario: Edita el numero para cambiar el primer valor
$SegundoValor = 30; //Comentario: Edita el numero para cambiar el segundo valor

//Seccion de calculos
$suma = $PrimerValor + $SegundoValor; //Sumamos
$resta = $PrimerValor - $SegundoValor; //Restamos
$multiplicacion = $PrimerValor * $SegundoValor; //Multiplicamos
$division = $PrimerValor / $SegundoValor; //Dividimos

//A continuacion mostramos las cuentas en un formato textual

echo "<h1>Formato Textual</h1>";
echo "La <strong>suma</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>";
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y
# Comentarios. # Ejemplo de programacion en PHP simple # Haciendo simples calculos entre variables predefinidas # Por EjemploDe.Com 
$PrimerValor = 10; //Comentario: Edita el numero para cambiar el primer valor $SegundoValor = 30; //Comentario: Edita el numero para cambiar el segundo valor 
//Seccion de calculos $suma = $PrimerValor + $SegundoValor; //Sumamos $resta = $PrimerValor - $SegundoValor; //Restamos $multiplicacion = $PrimerValor * $SegundoValor; //Multiplicamos $division = $PrimerValor / $SegundoValor; //Dividimos 
//A continuacion mostramos las cuentas en un formato textual 
echo "<h1>Formato Textual</h1>"; echo "La <strong>suma</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>"; echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>";
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$suma."</strong><br/>";
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>";
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>resta</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>"; echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>";
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$resta."</strong><br/>";
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>";
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>multiplicacion</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>"; echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y ".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>";
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y 
".$SegundoValor."</strong> es de <strong>".$multiplicacion."</strong><br/>";
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$division."</strong><br/>"; //A continuacion mostramos las cuentas en un formato matematico 


echo "<h1>Formato Matematico</h1>";
echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>";
echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>";
echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>";
echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>";
?>
echo "La <strong>division</strong> entre <strong>".$PrimerValor."</strong> y <strong> ".$SegundoValor."</strong> es de <strong>".$division."</strong><br/>"; //A continuacion mostramos las cuentas en un formato matematico 
echo "<h1>Formato Matematico</h1>"; echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>"; echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>"; echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>"; echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>"; ?> ".$SegundoValor."</strong> es de <strong>".$division."</strong><br/>"; //A continuacion mostramos las cuentas en un formato matematico

echo "<h1>Formato Matematico</h1>";
echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>";
echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>";
echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>";
echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>";
?>

echo "<h1>Formato Matematico</h1>"; echo "".$PrimerValor." + ".$SegundoValor." = <strong>".$suma."</strong><br/>"; echo "".$PrimerValor." - ".$SegundoValor." = <strong>".$resta."</strong><br/>"; echo "".$PrimerValor." * ".$SegundoValor." = <strong>".$multiplicacion."</strong><br/>"; echo "".$PrimerValor." / ".$SegundoValor." = <strong>".$division."</strong><br/>"; ?>
This example show the following in PHP
The sum of 10 and 30 is 40
The subtraction between 10 and 30 is -20
The multiplication between 10 and 30 is 300
The division between 10 and 30 is 0.333333333333

10 + 30 = 40
10 to 30 = -20
10 * 30 = 300
10 / 30 = 0.333333333333 






Sample PHP Class

class is part of Object-Oriented Programming (OOP, or English OOP) in PHP. A class is an object.
There are doing a Sample Class in PHP.
File: main.class.php 
[Code] <?
# File Class
WordClass class {
var $ text;
function __construct ($ number) {
echo "start class". $ number. ""
}
cambiarWord function ($ nuevotexto) {
echo "text". $ this-> text. "has been replaced by:";
$ This-> text = $ nuevotexto;
echo $ this-> text;
echo "<br/>";
}
}
?> [/ Code] 

File: index.php 
[Code] <?
# Main file where the class call
include ("main.class.php);
$ Chatter = new WordClass (1) / / show "to start the class: 1" Since we number 1 as a parameter
$ Mumbo-> cambiarWord ("Hello as you go");
$ Mumbo-> cambiarWord ("Lalala");
?> [/ Code]

And more later....

Install Smarty in Windows

We will report in this article the steps to install Smarty on a local server on Windows. For those who do not know, Smarty is a template system for PHP, we can separate the presentation (layout) of the code (programming), ie, separating the business logic created by scripts in PHP, HTML used to display information on the website. Thus, we have the PHP scripts that perform programmers, independently of the presentation made by designers. Smarty also offers many other advantages, such as caching generated pages, which can increase server performance.


Steps for installing Smarty

The first step is to enter the page Smarty to download the latest version of the product.We will have to unzip the downloaded file and then place it in any directory where you want, provided it is outside the directory publishing pages, bone, outside the document root. In my case, I have installed WAMP (Apache + package providing PHP + MySQL, with simple installation), I put Smarty in the Wamp installation directory but not in the directory "www", which is the directory publishing the pages.
The directory where I unpacked file is C: wampsmarty
Now we have to add in the PHP include_path path of the directory "libs" is in the directory where we put files Smarty (include_path directive, which is in the PHP php.ini indicates the directories where you will find the files to be included in PHP include function both as to require). To edit the php.ini, the first thing we have to do is locate it on your hard drive.


In the php.ini have to search the include_path directive above. The best thing is to look at the file with a text search tool.Then you have to modify it to add the directory where you installed Smarty libs. For example, you may get something like this:
include_path = ".; C: wampsmartylibs"
Once you have edited and saved php.ini, you have to restart your web server for the changes made in the include_path to take effect.
Now you have to create multiple directories using Smarty to store the source code templates, templates processed and the cache. There are 4 directories:
On the one hand we have two folders that must be stored somewhere on the server publishing directory. My publishing directory is: C: wampwww then, these two directories would be:
C: wampwwwsmartytemplates (This is the directory where you saved the templates)
C: wampwww smartyconfigs (This directory settings are saved Smarty)
On the other hand we have two files that should be kept out of our publishing directory.
C: wampsmartytemplates_c (in this directory are saved Smarty compiled templates)
C: wampsmartycache (in this directory cache stores the pages generated by Smarty)

Test the installation of Smarty

Once we have finished installing and configuring the system, we can do a test to see if everything works fine.To do this we must create two files: 1) a PHP file that uses the template and 2) the code itself to generate a Smarty template page.
First we will create the PHP file with the code needed to load, configure and display the template. This file can be placed anywhere in our directory publication. The PHP code is as follows:
<? Php 
/ / Load the smarty library 
require ('Smarty.class.php'); 
$ Smarty = new Smarty; 
$ Smarty-> template_dir = 'C: wampwwwsmartytemplates'; 
$ Smarty-> config_dir = 'C: wampwwwsmartyconfig'; 
$ Smarty-> cache_dir = 'C: wampsmartycache'; 
$ Smarty-> compile_dir = 'C: wampsmartytemplates_c'; 
/ / Assign values ​​to customize the template to replace the variables in the same 
$ Smarty-> assign ('name', 'DesarrolloWeb.com'); 
$ Smarty-> assign ('title', 'Title of the page that I put in PHP to customize!'); 
$ Smarty-> display ('index.tpl'); 
?>
As we can see, using a Smarty template, called "index.tpl" (the name of the template you are using can be seen in the line of code $ smarty-> display ('index.tpl');) , we also have to create and save in the directory you have created to store the templates C: wampwwwsmartytemplates.
<html> 
<body> 
<h1>} {$ title </ h1> 
Hello {$ name}! 
</ Body> 
</ Html>
To see if everything is working perfectly we must open the file where we put the first code PHP. I kept it in the root directory of the publication and the name "pruebosmarty.php." Then to open it through the web server, I have access to the following URL:
http://localhost/pruebosmarty.php

Troubleshooting the installation of Smarty

I can think of two errors that you can go.The first is that an error will include Smarty.class.php. If you encounter this error, you may not have edited the php.ini correctly, setting the include_path in the correct path of the directory "libs" Smarty files. Remember to restart the server whenever you make changes to the php.ini and the correct way to locate your php.ini on your hard drive (through the execution of the phpinfo ()).
Another mistake is that you can leave the system has not located the template you have to open. In this case check the line where you specify the directory templates in PHP code:
$ Smarty-> template_dir = 'C: wampwwwsmartytemplates';
And check that this directory is the template you're working "index.tpl".
I hope this helps, inspired Smarty's own documentation and expanded with additional explanations, drawn from the tests performed by desarrolloweb. Com, you can install the Smarty templating system for PHP.

WampServer. Install PHP5


There is a facility called WAMP5 package that can be installed together Apache, PHP 5, MySQL and PHPMyAdmin. This chapter also refers to the WAMP5 installation mode and other options to increase the chances of the package.

Platform:
Windows
Language: Multilanguage
License: Freeware
Size: 20 MB
Web: http://www.wampserver.com/

There is a way to start using PHP5 on Windows without having to suffer the typical complications of the installation of servers required to program in PHP. This is called WAMP install a package that lets you install and configure one process in the Apache server, MySQL database and PHP programming module in version 5. With WampServer, install PHP5has never been easier.

WAMP is a system suitable for users who do not have the system installed on any of the programs required to program in PHP (Apache, PHP and MySQL), and performing a complete PHP5 installation from scratch. But they can also use this program users with Apache, PHP and / or MySQL on your system. In which case, simply make another copy of the application in a different directory, which in principle does not necessarily interfere with the other facilities housed in our team.

Programs containing WAMP5

The software that is installed WAMP5 server and contains the following programs:
  • Apache 1.3.31. The web server market more widely. Although the latest version of this server is Apache 2, you install an earlier version that is more stable. There is an add-on that allows you to replace the Apache 1.3.31 version for the latest version.
  • PHP5. The new engine of language.
  • MySQL. The most widely used database for use with PHP.
  • PHPmyadmin. A software that lets you manage a database through a web interface.
  • SQLiteManager. A system for managing a database from SQL statements.
Installing WAMP

Installation is done through a Windows executable where you can enter a few settings, just the directory where you want to install the programs. After the installation process have created a couple of services with the web server and database:
  • Wampapache Service: Related to the Apache server.
  • Wampmysql Service: Related to the MySQL database.
Furthermore, within the directory where you have installed WAMP5 is created a folder named "www", which corresponds to the publishing directory, ie the place where you should place the web pages.

During installation must also decide if we want WAMP5 automatically start when you boot the computer or if you want your startup is performed manually.

Starting the servers

When you install WAMP5 creates a program group called WampServer, where we can find an option that says "Start Wampserver" which will need to run if we have not selected the server to start automatically.

Once started WampServer will have an icon on the taskbar with a shape similar to a speedometer. If you click on this icon opens a menu with various options to manage the services associated with the package. The image below shows the menu.



We test whether the services are running well by going to the server startup, writing in the address bar of your browser something like http://localhost/.

Then we should see a page with several links to various tools installed with WAMP5, plus some PHP test pages.

Add-ons

There are several additions that can be installed with WAMP, to extend the capabilities of the package. For example, we can install an add-on to allow working with WAMP PHP5 or PHP4, creating a new option in the WAMP5 menu allowing change between versions of PHP.

There are other additives available:
  • Installing ActiveState Perl on your system, to allow execution of CGI.
  • Upgrade to Apache 2.
  • Install Zend Optimizer to improve the runtime behavior of PHP.
  • Finally, the add-on you install Webalizer, a system to gather statistics about web server usage.
You can get more information from this system and options for download at http://www.wampserver.com/

Smarty 3.1.2, PHP templates engine


Smarty is a template engine for PHP. Smarty separates PHP code and business logic, HTML code, and 
presentation logic, and generates web content by the placement of Smarty tags within a document. It is under the LGPL license so it can be used freely.
It is common in large projects the role of graphic designer and programmer are covered by different people, but the PHP programming tends to combine these two tasks in a person within the same code which brings great difficulties When replacing any part of the design of the page, then you have to dig into scripts to modify the presentation of content, Smarty seeks to solve this problem. - Wikipedia

Requirements

  • Smarty 3.x: PHP 5.2 +
  • Smarty 2.x: PHP 4 or 5

Download

Download: Smarty 3.1.2

Nov 16, 2011

PHP Authentication using IIS 7.5

Configure IIS for PHP support

Summary


The creation of websites and web applications is commonplace today to capture ideas from people or companies, either in languages ​​such as PHP or ASP.NET.
When developing web applications in different languages ​​have to mount them on different servers, which caused us poor management of our websites.
The proposed article is how we can implement PHP and ASP.NET applications within a single IIS web server and use server functions to control access to our PHP application.
The result will help us avoid creating control modules for user authentication in our web application.
In conclusion, the interoperability between PHP and IIS does not provide better management and security of our websites.
Keywords:

Web: the system of documents (or web pages) interconnected by hypertext links available on the Internet. Web applications: A web application is any application that is accessed via web over a network like the Internet or an intranet. Web Server: A server that is dedicated to providing web related services, especially for a website is available online. PHP (PHP Hypertext Pre-processor). Programming language generally used in content creation for websites. ASP.NET: Microsoft is a technology that run scripts on the server and can be used to create dynamic and interactive applications on the Web.Apache: Web server for free distribution and open source. IIS: Microsoft Internet Information Server (or IIS) are software services that support the creation, configuration and administration of websites. Open Source (Open source). Designation for those applications that have their source code or program code released. Authentication: In computing, an act of establishing or confirming a user to a system as genuine. C # is a programming language and object-oriented standard developed by Microsoft as part of their platform. NET.
Introduction


Creating Web Applications is proposing to implement the requirements and objectives of Internet users or companies to provide services to users dare from a Web server, as these applications are publicly available as online stores, social networks or applications and more restricted as intranets for internal application management in a company, The development of these applications can be done using programming languages ​​such as PHP or ASP.NET Microsoft implemented on a web server can be Apache or IIS.
The development in PHP is extremely popular for being open source and available on all platforms, commonly used by applications to us as managers to be blogs, and CMS, PHP as a language has a syntax very similar to leguaje C, based primarily on the scripts to interact on the web, PHP is currently supported by PHP Group that receives input from multiple users for improvement but its main contributions are received from the company which I think ZEND engine ZEND Engine call that based on PHP.
The IIS Web server offers significant improvements in the field of Web services. The advances are motivated primarily by security and performance, including new security features take advantage of the latest encryption technologies and certificate authentication methods, and allowing the server to verify the client before the user log , better management of our web applications, denying access to the applications we have on known IP server, limiting bandwidth to our sites, add modules programmed applications to add sub sites, filtering applications and more.
The problem that usually occurs when developing web applications is to have a web server for each application, whether they are developed in PHP or ASP.NET both are implemented on a specific type of web server Apache or IIS being these , but because we can not integrate them into a single web server that we simplify the administration of our applications.
What Microsoft is proposing is to have different web servers installed, but to manage the concept of interoperability between applications, using interoperability enhancements brings the IIS web server, so we simplify the deployment of applications in PHP and ASP.NET single web server.
Deploying PHP applications in the IIS web server provides facilities in his administration, also offers new features which we apply to our work, as well as creating user accounts, user groups, security authentication and re-routing pages, only using our web server. Simplifying the creation of extra code in our application.
What we do not have to simplify the control modules for creating user authentication in our web application, which would be beneficial to us, because we can spend that time developing and improving our applications.
PHP applications on IIS
By deploying PHP applications on the web server must have a clear focus or basics of how to prepare our server for better performance and support for PHP applications, reliability and several that are the key aspects that should be assessed at the time of implementation.
-Support for PHP applications. Is generated through the partnership that Microsoft does with Zend to improve the interoperability of PHP on Windows platforms, thereby generating an application called php.exe and Fast-CGI supplement that improves the performance of our PHP applications on our server.
-Reliability is generated dare reliability of FastCGI that functions as an interface between PHP and IIS engine and ensures that PHP runs faster than CGI and a more reliable PHP ISAPI, FastCGI also gives us stability in the server when processing applications, providing better performance.
PHP application authentication managed by IIS
Managed by IIS authentication in PHP applications gives us a better management of our users and user groups, as using these features we can easily manage users in a Microsoft SQL Database Server 2008 or Active Directory.
For authentication of PHP applications on IIS correctly we have to be found FastCGI to run PHP applications on our web server.


1. The first thing we need to install the IIS log to Start> All Programs> Control Panel (Figure 1)> Programs (Figure 2)> Enable or disable Windows features
Figura06
Figure 1
Figura06
Figure 2


2. By clicking on or off Windows features will appear called Windows Features window (Figure 3), once released and we mark the Internet Information Services option, after you mark the option to deploy application development features (Figure 3 ) and checking CGI (Figure 4), once done we look for common HTTP Features option and deployed, we seek the Directory Browsing option or the mark (Figure 5), we once again made ​​Support web administration and mark complement IIS Management Console (Figure 6).
Figura03
Figure 3
Figura04
Figure 4
Figura05
Figure 5
Figura06


Figure 6


3. Having done all these options Click OK and we installed the IIS Web server on our team.

INSTALLING PHP ON WINDOWS

1. First we enter the main page at the following link PHP http://www.php.net/downloads.php and look for PHP 5.2.14 installer (Figure 7) and download it.
Figura07


Figure 7


2. Once we downloaded the file unzip it in the directory "C: \ PHP \" (Figure 9).
Figura08


Figure 8


3. Once unpacked in the directory entered in it, and seek in file C: \ PHP \ php.ini-recommendedand rename it to C: \ PHP \ php.ini.


Note: In the directory exists a file called php.ini-dist, but it is recommended to use the file php.ini-recommended because it has been optimized for performance and safety.


4. Then we assign read permissions to all users. For that we look for the file in the directory C: \ PHP \ php.ini select it and right click the mouse on the computer, then select the Security tab and look for the edit tab (Figure 9), once the pressure on it A window will appear which we seek the Add button (Figure 10) and another window will appear, write "All" (Figure 11) and then press OK, We window reappears php.ini file permissions where there will be a new user called "Everyone" and click OK.
Figura09
Figure 9
Figura10
Figure 10
Figura11


Figure 11


5. We have to include the Windows environment variables in PHP, to do press Start> Control Panel> System, then click Advanced system settings (Figure 12), we see a window called System Properties seek the Advanced tab, and press Environment Variables button (Figure 13), another window will appear (Figure 14) where we edit the system variables by pressing the edit button and add the variableC: \ PHP (Figure 15) after the semicolon without erasing any existing value and press OK.
Figura12
Figure 12
Figura13
Figure 13
Figura14
Figure 14
Figura15


Figure 15


6. Then we assign read and execute permissions to the folder PHP, for they seek the directory where you installed our PHP folder, once we place the folder right click on it and press properties, then select the Security tab and look for the tab edit (Figure 16), once the pressure in it will appear a window in which we seek the Add button (Figure 17) and another window will appear where you write "IIS_IUSRS"(Figure 18) and then press OK, we will window appears where there PHP permissions a new user called "IIS_IUSRS" and look for the Total Control option (Figure 19) we mark it and click OK.
Figura16
Figure 16
Figura17
Figure 17
Figura18
Figure 18
Figura19


Figure 19


7. Then edit a file by opening it with PWS-php5cgi.reg blog notes (located in C: \ PHP) to reflect the location of php-cgi.exe, leaving him with the following content.
PWS-php5cgi.reg
REGEDIT4   [HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ w3svc \ parameters \ Script Map] ". Php" = "C: \ \ PHP \ \ php.exe-cgi.exe"


After editing save the file and then execute it by double clicking on it.


8. To enable the snap of PHP (FastCGI) on IIS 7 must take the following steps:


a. Access the directory C: \ Windows \ System32 \ inetsrv \ config (We ask administrator permission and we continue).


b. Once we're in the folder to open the file applicationHost.config blog notes, and look for the following lines of code <fastCgi> </ FastCGI>, and replace it with the following.
applicationHost.config
<fastCgi>   <application fullPath="c:\PHP\php-cgi.exe" />   </ FastCGI>


9. Modifying the PHP configuration file (php.ini located in C: \ PHP).


a. We look in php.ini the following lines of code:
php.ini
cgi.fix_pathinfo fastcgi.impersonate = 1 = 1 cgi.force_redirect = 0


b. After changing the store and we must restart the IIS service with the following command, Start> Accessories> Command Prompt, we make a right click and look for the Run as administratoroption, once you open the command iisreset write the following statement and press enter, then close it.


Note: We removed the comment that is defined with a semicolon ";".

SET PHP on IIS 7



1. We began to set Server IIS 7.


a. We entered our IIS, Start> Accessories> Run (Figure 20), and type inetmgr and accept.
Figura20


Figure 20


b. A window will appear (Figure 21) which is the manager of IIS 7, we look for and click on "Handler Mappings".
Figura21


Figure 21


c. After clicking a window will appear where you can assign control of modules (Figure 22), in the press"Add module assignment."
Figura22


Figure 22


d. Sub-window will appear called "Assign allocation module" (Figure 23), where we add the following values:
RequestValue
Path of applications:*. Php
Module:FastCgiModule
Executable (Optional):C: \ PHP \ php-cgi.exe
Name:PHP_via_FastCGI
Figura23


Figure 23


e. We will see a message asking if you want to create a FastCGI application executable (Figure 24), and press Yes.
Figura24


Figure 24


2. Once configured, the IIS7 with the above, and we support PHP applications on our platform.

IIS7 managed authentication IN PHP

1. First we have to activate Windows features on our IIS7 server. We entered to Start> Control Panel> Programs> Enable or disable Windows Search feature, a window appears instead of Windows features and we mark the Internet Information Services option, after you mark the option to deploy Web Services World Wide and deploy the Security tab and mark the security options as pictured (Figure 25), once we made ​​the choice of application development features and deploy, and mark options (Figure 26).
Figura25
Figure 25
Figura26


Figure 26


2. Create a new folder called login in the directory c: \ inetpub \ (Figure 27)
Figura27
Figure 27


3. We entered our IIS7, Start> Accessories> Run (Figure 20), and type inetmgr and click OK.


4. After entering the IIS7, look for the places we will file an action menu where you press Add Web Site ... (Figure 28).
Figura28


Figure 28


5. Once down we see a window called Add Web Site ... in which we assign the following values:
RequestValue
Site Name:login
Physical path:C: \ inetpub \ login
Port:82


Note: We can assign IP if you want, but for convenience we let the default instance.


Once you add the values ​​(Figure 29) press OK.
Figura29


Figure 29


6. Once created, login our site will appear as in Figure 30, and double click it.
Figura30


Figure 30


7. Being on our site we go to the Authentication tab (Figure 31), which will display the attributes we seek in Forms Authentication (Figure 32) and empowering and should be as in Figure 33.
Figura31
Figure 31
Figura32
Figure 32
Figura33
Figure 33
8. We return to the menu of our site and look for the option modules (Figure 34) and enter it, once we look at the FormsAuthentication option (Figure 35) pressed on it and we will get a window calledChange Manager module and verify that this enabled option (Figure 36) Calling for applications for ASP.NET applications or managed handlers.
Figura34
Figure 34
Figura35
Figure 35
Figura36


Figure 36


9. We return to the menu of our site and look for the users choice. NET (Figure 37) and enter it (Figure 38) we generate two files one with extension MDF and LDF files with the extension in the directory C: \ inetpub \ login \ App_Data \, which is the database to work with our application, which will store the user accounts.
Figura37
Figure 37
Figura38


Figure 38


a. Once created these files, download and install (How to install SQL Server 2008http://tinyurl.com/369bcal ) SQL Server 2008 R2 this Advance Services and generate a mixed authentication when installing the SQL Server, setting password for the sa user 1234 in our database.


b. Back to our menu and look for the Connection String option (Figure 39), which we will edit to avoid creating problems connecting to our users.
Figura39


Figure 39


c. We entered it we see a string with the name LocalSqlServer (Figure 40) click on it and we will get a window called Modify connection string (Figure 41) click on Custom and enter the following information:
Connection String
Source =. \ SQLEXPRESS; AttachDBFilename = C: \ inetpub \ login \ App_Data \ aspnetdb.mdf; Initial Catalog = login_ASP, User ID = sa; Password = 1234  
That way we will have no trouble creating users for our application.
Figura40
Figure 40
Figura41


Figure 41


10. We return to the menu of our site and look for the users choice. NET (Figure 37) and enter it (Figure 38) we seek in the action menu Add ... we will get a window called Add User. NET (Figure 42), I enter the following information.
Username:test
Email:test@test.com
Password:test123 *
Figura42


Figure 42


11. We return to the menu of our site and find the option Authorization rules. NET (Figure 43) and we we will get a window entitled Add permit authorization rule (Figure 44), and mark all anonymous users to accept.
Figura43
Figure 43
Figura44


Figure 44


12. Create two folders for our example to work and admin folder App_Data.


13. Create a file on your desktop called login.aspx with the following code in a blog of notes and save it with the "aspx". And copies it to the directory C: \ inetpub \ login \
login.aspx
<head> <html> income <title> Page </ title> </ head> <body>
<form runat="server">
  / / Load the form to login.
<asp:Login runat="server" />
</ Form>
</ Body>
</ Html>  
14. Create a file on your desktop called usuario.php with the following code in a blog of notes and save it with the extension "php". And copies it to the directory C: \ inetpub \ login \
usuario.php
<head> <html> <title> Welcome </ title> </ head> <body>
Welcome to my page <h2> PHP </ h2>
<h1>
<? Php / / Print the user name?>
  <? Php print ($ _SERVER ['LOGON_USER']);?>.
</ H1>
<p> Their role is:
<? Php / / Print the name of the session of our class created PHPRoles.cs?>
  <? Php print (isset ($ _SERVER ['AUTH_ROLES'])? $ _SERVER ['AUTH_ROLES']: "");?>
</ P>
Today is <p>
<? Php / / Print the date?>
<? Php print (date ("l F d, Y"));?>.
Exit <a href="logout.aspx"> page </ a>
</ P>
</ Body>
</ Html>  
a. Create a file on your desktop called usuario.php with the following code in a blog of notes and save it with the extension "php". And copies it to the directory C: \ inetpub \ login \
Logout.aspx
<Script runat = "server" language = "c #"> void Page_Load (Object source, EventArgs e) {/ / Exit FormsAuthentication.SignOut ();
/ / Returns the previous page
  ReturnUrl string = Request.QueryString ["ReturnUrl"];
if (String.IsNullOrEmpty (ReturnUrl))
{
Request.Headers ReturnUrl = ["Referer"];
}
if (String.IsNullOrEmpty (ReturnUrl))
{
Response.Redirect (ReturnUrl);
}
}
</ Script>  


b. We create our directory C: \ inetpub \ login \ a new folder named App_Code


c. Create a file on your desktop called PHPRoles.cs with the following code in a blog of notes and save it with the extension "php". And copies it to the directory C: \ inetpub \ login \ App_Code
PHPRoles.cs
using System; using System.Web; using System.Web.Security;   namespace control
{
/ / Create the event with IHttpModule
public class ModuloRolesPHP: IHttpModule
{
public void Dispose ()
{
}
public void Init (HttpApplication app)
  {
/ / The event PostAuthorizeRequest, authentication and guaranteed
  / / Authorization for the application
+ = new EventHandler app.PostAuthorizeRequest (ExtractRoles);
}
private void ExtraeRoles (Object source, EventArgs args)
{
HttpApplication app = (HttpApplication) source;
rp = app.Context.User rolprincipal as rolprincipal;
if (rp! = null)
{
string [] roles = rp.GetRoles ();
  / / Get the server roles that we have configured IIS
  if (roles! = null & & roles.Length> 0)
{
/ / Create server variables
app.Context.Request.ServerVariables ["AUTH_ROLES"] = String.Join (",", roles);
}
}
}
}
}  
15. Introducing the web browser address http://localhost:82/usuario.php (Figure 45) press enter once set and we redirected to a login page so we can log it http://localhost:82/login.aspx?ReturnUrl =% 2fusuario.php
Figura45


Figure 45


i. When we leave the authentication page, sign in with the user created above
Username:test
Password:test123 *
ii. After logging redirect us to our php page (Figure 46)
Figura46


Figure 46


a. Now press Exit Page, and we will redirect to the authentication page.


16. Now we assign a role to our users, go to our IIS, Start> Accessories> Run (Figure 20), and typeinetmgr and accept.


i. Roles seek. NET (Figure 47) and double click
Figura47
Figure 47
ii. Add a user role (Figure 48)
Figura48


Figure 48


iii. Enter a role name (Figure 49), in this case we put admin and we accept.
Figura49


Figure 49


iv. Roles will appear. Net (Figure 50) the role that we create.
Figura50
Figure 50
v. We return to the main menu and look for IIS7 modules (Figure 51) and entered in the
Figura51


Figure 51


vi. Within the same pressure in the action menu Add Managed Module ... (Figure 52)
Figura52


Figure 52


vii. When we leave the Add Managed Module (Figure 53) select the tab and look for the class that we created and assigned the control name and click OK.
Figura53


Figure 53


viii. We return to the Users menu. NET and we have modified the user pressing the Edit ... (Figure 54) the action menu.
Figura54


Figure 54


ix. We open a window where the new role we have created, then select the admin role (Figure 55) and press OK, and our role will belong to user already created.
Figura55


Figure 55


17. Create a file on your desktop called admin.php with the following code in a blog of notes and save it with the extension "php". And copies it to the directory C: \ inetpub \ login \ admin, to show us and the roles in order to use our php page.
admin.php
<? Php echo ("Site Administrator");?>  
a. Create a file on your desktop called web.config with the following code in a blog of notes and save it with the "config". And copies it to the directory C: \ inetpub \ login \ admin
web.config
<? Xml version = "1.0" encoding = "UTF-8"?> <configuration> <system.webServer> <security> <authorization>
verbs="" roles="" users="*" <remove />
verbs="" roles="" users="?" <remove />
roles="Admin" accessType="Allow" <add />
</ Authorization>
</ Security>
</ System.webServer>
</ Configuration>  
18. After you have created all the files we can see that when entering our site (Figure 56) and can handle roles. NET.
Figura56


Figure 56


19. After finishing these steps do we finished building our sample application with IIS7 managed authentication in PHP download the code from CodePlexhttp://codeplex.codeplex.com/workitem/25660 , With this example we improving security our application PHP and having a better interoperability of platforms

REFERENCES

Configuring PHP


http://www.ignside.net/man/servidores/phpini.php
http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-7/
http://www.webtaller.com/construccion/lenguajes/php/lecciones/que-es-archivo-php.ini.php
http://cvs.php.net/
http://www.ignside.net/man/servidores/phpini.php
http://www.php.net/downloads.php

Authentication in IIS


http://www.iis.net/ConfigReference/system.webServer/security/authentication/basicAuthentication
http://www.iis.net/ConfigReference/system.webServer/security/authentication/windowsAuthentication
http://support.microsoft.com/kb/2002980/es
http://technet.microsoft.com/es-es/library/cc771077 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc731595 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc754257 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc772290 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc754881 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc732830 (WS.10). aspx
http://blog.jmacoe.com/miscelanea/seguridad/hack-expone-seguridad-autenticacion-formularios-aspnet/

http://forums.asp.net/p/1008155/1509684.aspx

http://msdn.microsoft.com/es-es/library/system.web.httpapplication.postauthorizerequest (VS.90). aspx

Users. NET


http://msdn.microsoft.com/en-us/library/532aee0e.aspx
http://msdn.microsoft.com/en-us/library/1d3t3c61.aspx
http://msdn.microsoft.com/en-us/library/e01fc50a.aspx
http://social.msdn.microsoft.com/Forums/es-ES/netfxwebes/thread/275d9c5e-26d2-4bb3-99ab-4d477da771de
http://technet.microsoft.com/es-es/library/cc730708 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc732215 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc730621 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc731278 (WS.10). aspx
http://technet.microsoft.com/es-es/library/cc754705 (WS.10). aspx
http://www.youtube.com/watch?v=yXO2xrSMHM4

Installing SQL Server 2008 Microsoft SQL Server 2008 Express with Advanced Services



http://msdn.microsoft.com/en-us/library/ms143219.aspx
http://msdn.microsoft.com/es-es/library/bb500395.aspx


http://www.sqlserverclub.com/essentialguides/how-to-install-sql-server-2008-step-by-step-guide.aspx


http://vimeo.com/9315795


http://www.compucaja.com.mx/ayuda/INS003.pdf


................Apologized for pictures

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes