• bezpečný...
  • stabilný...
  • rýchly!

Programming support

section: Support

Sending an e-mail from a PHP script

For sending an e-mail message from a PHP script we recommend to use the class Mail from the PEAR Mail.php package.

require_once 'Mail.php';
$mail = Mail::factory('mail');
$body = 'This is a message text...';
$hdrs = array(
  'From'    => 'sender@example.com',
  'Subject' => 'Message Subject'
);
$mail->send('recipient@example.com', $hdrs, $body);

For more configuration options you can read documentation related to this class:
http://pear.php.net/package/Mail/docs

Downloading a page from a remote server

For downloading a page from remote server, the simplest and most efficient way is to use the PEAR class HTTP_Request from the HTTP/Request.php package.

require_once 'HTTP/Request.php';
$url = 'http://platon.sk/';
$req =& new HTTP_Request($url);
if (PEAR::isError($req->sendRequest())) {
  die('error downloading page');
}
$content = $req->getResponseBody();

For more options and functionality, please read documentation at:
http://pear.php.net/package/HTTP_Request/docs

Redirecting to a single domain

If your website is running under several domain names, it is recommended to choose one main domain and make the redirection from other domains to the selected one. Place this part of the code at the beginning of your PHP script.

$redirect  = 'platon.sk';
$http_host = @$_SERVER['HTTP_HOST'];
$req_uri   = @$_SERVER['REQUEST_URI'];
if (strlen($http_host) > 0
        && strcasecmp($http_host, $redirect))
{
    header("Location: http://$redirect$req_uri");
    exit;
}   

The main advantages of this solution are mostly within the area of Search Engine Optimization (SEO):

  • the same content is placed on a single website (domain), what most browsers really like;
  • Google PageRank value is not divided between several pages, but rather "summarized" together for the one main page;
  • as a result of these steps, indexation of your website should improve as well as its ranking in search engines.

HTTPS Redirect

If you want your page to run only under secure HTTPS protocol, you can put following lines into your .htaccess file. Apache will redirect all future requests from HTTP to HTTPS.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{ENV:HTTPS} !^on$
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
</IfModule>

More .htaccess redirects

If we need to redirect more different URLs by query address to an unique address, or we need special rules, below, we are showing tips that can be used, with explanation.

  • [OR] menas, that next rules are also evaluated, if current rule was evaluated as false (pattern does not match with input)
  • %{QUERY_STRING} is, what is on the end of url after question mark - as shown below in example -if URL looks like https://domain.ext/hocijaka-cesta/?kontakt then the matching string to evaluate will be word "kontakt" (without quotes)
  • RewriteRule will rewrite URL by scheme. First is the rule for the original path - for example, we used only ^ - this means, that original url should be the protocol, then domain name, then RewriteBase.
    If RewriteBase is set to / (RewriteBase /), i tcan look then like this: http(s)://domain.ext/ - this rule is applied only to URL that begins on this pattern.
    Next item is path of new URL. Can be absolute (does not begin with slash) - for example http(s)://new-domain.ext/super-content, or relative (begins with slash)
  • L as last means, that evaluation ends with this rule.
  • R as redirect means, that the page will be redirected. There is used type 301 for permanent redirect, or 302 for temporary. 301 is good for searching services with stats (for example google) and the service uses stats for specific URLs. In case of using 301, the stats of new URL will be linked to stats of old URL. But, if 302 is used, then it will not happen (linking stats) and linking will be waiting, while there will be a new redirect with 301 code (this depends on service). Also 302 redirect is not stored in "cache" and can be easily changed unlike 301.
  • [NE] means, that URL should not be encoded - for example, space is converted to %20 and if in URL, there is a space, and NE is not specified then instead of sending "URL/nie co" the system wil send "URL/nie%20co" but if NE is set, the system will correctly send "URL/nie co".
  • In RewriteRule it is possible to use parenthesis around regular pattern and use content in target url. For example, if we want to change only part of url, we can use (as used in example below), the pattern (.*) - parenthesis will save the regular expression data to variable, and we will be then able to use it in target url with $1.

#URL, that match is http(s)://domain.ext/?kontakt(y) - redirect to http(s)://domain.ext/kontakt/
RewriteCond %{QUERY_STRING} ^kontakt$ [OR]
RewriteCond %{QUERY_STRING} ^kontakty$
RewriteRule ^ /kontakt/ [L,R=301]

#Redirecting from http(s)://domain.ext/?sidlo to http(s)://domain.ext/kontakt/#sidlo RewriteCond %{QUERY_STRING} ^sidlo$ RewriteRule ^ /kontakt/#sidlo [NE,L,R=301]

#Redirecting from http(s)://domain.ext/kontakt/bratislava?pobocka to #http(s)://domain.ext/kontakt/#bratislava pomocou 302 presmerovania (docasne) RewriteCond %{QUERY_STRING} ^pobocka$ RewriteRule ^kontakt/bratislava /kontakt/#bratislava [L,R=302]

#Redirecting from http(s)://domain.ext/kontakt/sidlo to http(s)://domain.ext/kontakt/ RewriteRule ^kontakt/sidlo /kontakt/ [L,R=301]

#Redirecting everything that begins with http(s)://domain.ext/cz/kontakt/ and redirecting to #http(s)://domain.ext/kontakt/cz/ - after last slash, remaining URL part will be attached RewriteCond %{QUERY_STRING} ^extra$ RewriteRule ^cz/kontakt/(.*)$ /kontakt/cz/$1 [NE,L,R=301]


If you would like to see more programming tips on this place, please let us know on support@platon.org e-mail address.

  • Platon Webhosting
  • Metafox CMS
  • Platon Technologies, s.r.o.