php - http referer和preg_match

I'm currently trying to work with $_SERVER['HTTP_REFERER'], but i don't get this to work...

Heres my code:

<?php
session_start();
error_reporting(E_WARNING | E_PARSE | E_ERROR);
date_default_timezone_set('Europe/Berlin');

if (isset($_SERVER['HTTP_REFERER']))
    $ref        = $_SERVER['HTTP_REFERER'];
else
    $ref = '';
$id = 0;

if (preg_match('~http://www\.domain1\.com~', $ref))
    $id = 1;
else if(preg_match('~http://www\.domain2\.com~', $ref))
    $id = 2;    
else if(preg_match('~http://www\.domain3\.com~', $ref))
    $id = 3;
else if(preg_match('~http://www\.domain4\.com~', $ref)  )
    $id = 4;

echo $id;   
?>

Any Idea why this isn't working?

Change all your regex patterns, '~' to '/' and try again. You also need to slash your '/', see example.

As in:

if (preg_match('/http:\/\/www\.domain1\.com/', $ref))
    $id = 1;