如何将用户重定向到他们自己的个人资料页面?

All users have their own profile page:

www.mysite.com/profile.php?id=13

What I am trying to do now is redirect them to their own profile page in case they only type:

www.mysite.com/profile.php

I've tried this but I keep on getting a redirect loop:

 if ((preg_match("{(?:profile)\.php$}", $_SERVER["PHP_SELF"])) && 
 (!empty($_SESSION["id"])) )
     {
         redirect ("profile.php?id=$id");        
     }

Am I missing a step?

$_SERVER["PHP_SELF"] will always return profile.php in case it is appended with id or not ie. why your regex always matches fine and you are moving in a redirect loop forever

Why dont you make the regex like this:-

if ((preg_match("/^\/profile\.php$/", $_SERVER[REQUEST_URI])) && 
 (!empty($_SESSION["id"])) )
     {
         redirect ("profile.php?id=$id");        
     }

$_SERVER['PHP_SELF'] contains the filename of the currently executing script, so your condition will always match.

I would use something like:

if ( empty($_GET['id') && !empty($_SESSION['id']) )