Does anyone could help me how to make Referral Link Script using PHP Scripting + MySQL Database ?

Example :

Details :

:: Referral ID using number or email
:: After someone make a registration, new referral will be make & save to the MySQL database automaticly
:: If the number or email doesn't exist in database, the link will go to index.php automaticly without referral
:: Referral link only protect by PHP script & not protect by any other script.

If someone know, please teach me ... cause I'm very confuse to make this script within this week
Thanks for help me ...