Jump toUpdate content

How to configure PHP

Reviewed on 05 August 2021Published on 05 August 2021

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();

?>
Note:
  • The default version of PHP for all webhostings is PHP 5. During the creation of your webhosting, a phpinfo.php file will be created.

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.

Tip:

Let us see how to use sessions with PHP4:

  1. Log into your PHP session using FTP and go to the folder www/ in which you will add the code of your website.

  2. Create a new file and name it start.php containing the following lines:

    ```
    <?php
    session_start();
    session_register ("count");
    $count = 42;
    echo "We registered ".$count." <br>";
    ?>
    To go to the next page <A HREF="nextpage.php">click here</A>

    ```
  3. Create a new file and name it nextpage.php containing the following lines:

    ```
    <?php
    session_start();
    session_register("count");
    echo "The previously registered count is " . $count ."<br>";
    ?>
    ```
  4. Upload the files to your website and open the file start.php in your browser.

How to configure a root directory (DocumentRoot)

Important:

Never use the absolute path hard coded in your site, because the path is likely to change and your site would be inaccessible.

  1. Use the variable $DOCUMENT_ROOT that points to the root of the website in which your script is being hosted.
  2. Use $path = “$DOCUMENT_ROOT/afolder/anotherfolder/”; if you want to know the path of your website http://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.

  1. Create a directory include (all lowercase letters) in the website’s folder (for example: www/include).

    Note:
    • Such directory can only be used within the website it was created in. This means the main website’s directory which is named www/include is not accessible from a secondary website such as blog.domain.ext).
  2. Below is an example of a directory-inclusion:

  • Let us suppose you created a file global.php including different information or methods.

  • You want to access it from anywhere on your website without copying it in each subdirectory or enter the relative path to that file in each program.

  • Let us assume that your .php files are located in the main website www.mydomain.ext>

  • You have to create www/include and store your file global.php in it (it is therefore in www/include/global.php seen in FTP)

  • Call it from a .php file regardless of where it is located inside the main website by writing:

      ```
    <?php
    include("global.php");
    // suite du code...
    ?>

    ```
  • You can also use:

<?php
require("global.php");
// more code to follow...
?>

How to connect to a database

Note:
  • Persistent connections are not possible due to the architecture of the webhosting platform and are performed as a standard connection.

Refer to the documentation to configure the connection to a database using PHP.

How to upload files?

Note:
  • The maximum file size you can upload is 10 MB.
  1. Rename an uploaded file using the function move_updloaded_file before the end of your script.
  2. The temporary file phpXXXXX will 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( isset($_POST['upload']) ) // if form is submitted
{
$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 2MB
  • 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.";
}

?>
Important:
  • Be aware that a malicious individual can use contact forms to send you spam. By calling several times a second, it will eventually saturate the contact@domain.ext email address you specified. The addition of a captcha can help you avoid this kind of hacking, and thus the suspension or cancellation of your account.

How to create a contact form (form2mail)

Let us suppose that the domain in this example is domain.ext.

  1. Create a file form.html and 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>
    ```
  2. Create a second file form2mail.php and 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
Important:

Versions prior to PHP 5.6 are deprecated, so we recommend you update the code of your website. If you need an older version, you must contact the support and request it.

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:

  1. Log into your account.

  2. Click Hosting, then Manage next your webhosting.

  3. Click Manage in the menu on the right. The Website configuration page displays.

  4. Click the Edit action.

  5. Select the PHP version of your choice.

  6. Click Submit to update your PHP version.

How to configure PHP

Important:

Configuring PHP is possible for all our offers. However, ensure you are running at least PHP version 5.3 and above.

  1. Upload a .user.inifile to the folder corresponding to your sub domain (folder /www for example).
<Message type="note">

The majority of options are configurable, except for those with an impact on the resources of the server.

</Message>
  1. You can modify all values with PHP_INI_ALL changeable option you can find by clicking this link.
Important:
  • chown: modifications of the owner of a file are prohibited. Files uploaded using PHP or FTP already belong to your user.

  • system/exec/popen: the execution of binaries or CGI files is not possible for security reasons.

  • ASP is not supported. The execution of CGI scripts (cgi-bin) and other programs is disabled.

How to solve a 500 Error in the htaccess file

You can get this error because of your .htaccess file has errors, such as:

  • Presence of unauthorized Directive
  • Presence of a syntax error
  • .htaccess File transferred in binary, instead of a transfer as text
  • Lack of a final blank line
  1. Rename the .htaccess file to htaccessX.txtto solve these errors.
  2. Create an empty .htaccess file.
  3. 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

  1. Update your CMS if it uses the phpmailer class (XOOPS, WAnewsletter for example).
  2. Replace the phpmailer directory present in your CMS with the latest version proposed here https://github.com/PHPMailer/PHPMailer.