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:
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.
At the end of our role will be as follows:
This would be an example of PHP mail in HTML format:
Example PHP Function
[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.
Password Generator Example
[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.
# 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]
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.
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]
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]
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
2: Example with file_get_contents
Example of random numbers and letters
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] <? PhpAs 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
# Ejemplode.com
echo "text file file.txt <strong>: </ strong>";
include ("file.txt");
?> [/ Code]
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
Example of Uploading Files
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
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
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.
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.
0 comments:
Post a Comment