How to configure PHP
How to configure PHP at Scaleway
phpinfo is a function of the PHP language, that allows you to see the active modules, the configuration of the server, the restrictions, and the compilation parameters of the language:
<?php
// Display information about the PHP version used:
echo phpinfo();
?>How to use sessions
Sessions are a method to save certain information when you are browsing, (your shopping cart during a pending order, for example). The session system is a default feature of PHP.
Let us see how to use sessions with PHP4:
- Log into your PHP session using FTP and go to the folder
www/in which you will add the code of your website. - Create a new file and name it
start.phpcontaining the following lines:<?php session_start(); session_register ("count"); $count = 42; echo "We registered ".$count." <br />"; ?> To go to the next page <A HREF="URLexample/nextpage.php">click here</A> - Create a new file and name it
nextpage.phpcontaining the following lines:<?php session_start(); session_register("count"); echo "The previously registered count is " . $count ."<br />"; ?> - Upload the files to your website and open the file
start.phpin your browser.
How to configure a root directory (DocumentRoot)
- Use the variable
$DOCUMENT_ROOTthat points to the root of the website in which your script is being hosted. - Use
$path = “$DOCUMENT_ROOT/afolder/anotherfolder/”;if you want to know the path of your websitehttp://www.mysite.ext/afolder/anotherfolder.
How to configure a directory inclusion (IncludePath)
As part of the creation of a large website, it is possible to centralize frequently included files by adding a default directory to the search list of included files. You must create it, as it is not created by default.
- Create a directory
include(all lowercase letters) in the website's folder (for example: www/include). - Below is an example of a directory-inclusion:
- Let us suppose you created a file
global.phpincluding different information or methods. - You want to access it from anywhere on your website without copying it in each subdirectory or entering the relative path to that file in each program.
- Let us assume that your
.phpfiles are located on the main websitewww.mydomain.ext>. - You have to create
www/includeand store your fileglobal.phpin it (it is therefore inwww/include/global.phpseen in FTP). - Call it from a
.phpfile regardless of where it is located inside the main website by writing:<?php include("global.php"); // following code... ?> - You can also use:
<?php require("global.php"); // more code to follow... ?>
- Let us suppose you created a file
How to connect to a database
Refer to the documentation on how to configure the connection to a database using PHP.
How to upload files
- Rename an uploaded file using the function
move_updloaded_filebefore the end of your script. - The temporary file
phpXXXXXwill be deleted at the end of the receiving PHP script. Below is an example:<form method="post" enctype="multipart/form-data" action="upload.php"> <p> <input type="file" name="file" size="30"> <input type="submit" name="upload" value="Upload"> </p> </form> <?php // if form is submitted if (isset($_POST['upload'])) { $tmp_file = $_FILES['file']['tmp_name']; $name_file = $_FILES['file']['name']; if (!is_uploaded_file($tmp_file)) { exit("The file can not be found"); } if (!move_uploaded_file($tmp_file, $name_file)) { exit("Impossible to move the file to $name_file"); } echo "The file has been uploaded and can be found at $name_file"; } ?>
How to send emails
The email function of PHP is available, but it has some limitations:
- No more than 35 recipients per call of the function,
- Maximum 500 outgoing mails per day, with a limitation of 60 mails/hour,
- Size of the mails is limited to 2 MB,
- Antispam detection,
- The mail function returns TRUE on success and FALSE if one of these conditions is not met.
Here is an example:
- Let us suppose that the domain is
domain.ext.
<?php
// Put here your valid email address
$to = "contact@domain.ext";
// Subject of the message
$subject = "Test mail() function of PHP";
// Body of the message, text encoded using iso-8859-1
$message = "Hello,\nthe message was send. Regards the Webmaster\n";
// Headers of the message
$headers = ""; // we clear the variable
$headers = "From: Webmaster <webmaster@domain.ext>\n"; // Adding the From field
// $headers = $headers."MIME-Version: 1.0\n"; // Adding the MIME version
$headers = $headers."Content-type: text/plain; charset=iso-8859-1\n"; // Add the type of encoding
// Call the mail function
if ( mail($to, $subject, $message, $headers) == TRUE )
{
echo "Mail sent.";
}
else
{
echo "Error: The message could not be sent.";
}
?>How to create a contact form (form2mail)
Let us suppose that the domain in this example is domain.ext.
- Create a file
form.htmland upload it using FTP. Your file should be similar to:<html> <body> <form action="form2mail.php" method="post"> Enter your email address: <input type="text" name="email"><br />Message:<br /> <textarea name="message" rows="8" cols="50"></textarea><br /> <input type="submit" value="Send the mail"> </form> </body> </html> - Create a second file
form2mail.phpand upload it using FTP. Your file should be similar to:<?php /* Initialization of the variables */ $from = "webmaster@domain.ext"; // the sender, replace domain.ext with your domain name $to = "you@domain.ext"; // recipient, put your mail address in here /* Preparation */ $subject = "Test of the mail() function of PHP"; // The subject of the mail $email = NULL; $message = NULL; /* Get the content of the email field */ if (!empty($_POST['email'])) { $email = $_POST['email'] ; } /* Get the content of the message field */ if ($email && !empty($_POST['message'])) { $message = "Message sent from $email :\n" . $_POST['message']; } /* Sending*/ if ($email && $message) { /* Required headers for the mail */ $headers = "From: Webmaster <$from>\n"; $headers .= "To: Contact <$to>\n"; /* $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-type: text/plain; charset=iso-8859-15\n"; /* Call of the mail function */ if (!mail($to, $subject, $message, $headers)){ echo "Error: Impossible to send the mail"; } else { echo "Email sent"; } } else { echo "Error: You need to specify a mail address and a message.\n"; } ?>
How to configure PHP versions
Scaleway offers different PHP versions:
- The version 5.5
- The version 5.6
- The version 7.0
- The version 7.1
- The version 7.2
- The version 7.3
The default version for .php is 7.3 for current webhosting accounts. For older accounts the version can be changed directly from the Dedibox console:
- Log into your account.
- Click Hosting, then Manage, next to your webhosting.
- Click Manage in the menu on the right. The Website configuration page displays.
- Click the Edit action.

- Select the PHP version of your choice.
- Click Submit to update your PHP version.
How to configure PHP
- Upload a
.user.inifile to the folder corresponding to your subdomain (folder/wwwfor example). - You can modify all values with
PHP_INI_ALLchangeable option you can find by clicking this link.
How to solve an HTTP error 500 in the htaccess file
You can get this error because your .htaccess file has errors, such as:
- Presence of unauthorized directive,
- Presence of a syntax error,
.htaccessfile transferred in binary, instead of a transfer as text,- Lack of a final blank line.
- Rename the
.htaccessfile tohtaccessX.txtto solve these errors. - Create an empty
.htaccessfile. - Add the lines one by one until you find the lines that caused the error 500.
How to solve an error in a PHP Script
An error 500 can come from an error in the PHP code.
Change the error_reporting and display_errors in a user.ini file as described in How to configure PHP.
How to solve email problems
- Update your CMS if it uses the
phpmailerclass (XOOPS, WAnewsletter for example). - Replace the
phpmailerdirectory present in your CMS with the latest version proposed here https://github.com/PHPMailer/PHPMailer.