Hi All,

It has been a long while since I've been here, but I have hit a wall/mind block/ whatever you want to call it.

This should be simple, but drawing a complete blank.

I have an affiliate website where my clients can refer us to their friends and receive our gratitude through free products... But I need to be able to track who referred who.

I've done it years ago, but not remembering how.

I want to use the base domain so it goes to index.php

so, it would be DomainName.com/'Username' where 'Username' is unique to each client.

When someone goes to that URL, I need to be able to capture that 'Username' in a variable in the index.php script so I can use it to access the DB to record the referral. But at the same time, they need to be able to view the index.php page to enter the site.

Probably a long explanation for a simple request.

Could someone head me in the right direction?

I've invested almost 10 hours searching the web and reading numerous articles, etc and feel like I know less now about how to do this then I did before I started...

Thanks in advance for anyones positive feedback and direction.

Douglas

Recommended Answers

All 3 Replies

So basically what you want is to be able to pass a parameter into index.php. For example, domain.com/index.php?username=dani or domain.com/index.php?username=fred

Your PHP code can then use $username = $_REQUEST['username']; in order to retrieve the username that is passed into PHP.

You'll then want to use .htaccess and mod_rewrite (You guessed it!!) to translate domain.com/dani into domain.com/index.php?username=dani

This will only work if you are using an Apache web server, not nginx, since only Apache supports .htaccess files.

If you don't already have an .htaccess file, create one in your domain root directory. It can look something like this:

RewriteEngine on

RewriteRule ^([a-z+])$ /index.php?username=$1 [L]

The above will quietly redirect something such as domain.com/abc to index.php?username=abc where abc is any string of one or more lowercase characters.

commented: Hey Dani, thanks for the response... +5

Dani,

I continued searching the web and actually a lot of posts in here from several years ago after posting that question, and found what I think was the answer I was looking for.

I didn't want to use a variable assignment on the end of the URL... ( /?m=username) I simply wanted to use the Domainname.com/Username

I was able to accomplish that with the .htaccess file like this:

php_value session.gc_maxlifetime 14400
RewriteEngine On
RewriteRule ^([a-z0-9]+)$ /index.php?m=$1 [NC,L] # captures member username no case last rule
ErrorDocument 404 http://www.JustDouglas.com/badlink.php

Then in the index.php script:

 if (isset($_GET['m'])){//If arriving with username in url assign to $ref
    $ref_hta = $_GET['m'];  // gets referrer from .htaccess
    $ref = filter_var($ref_hta, FILTER_SANITIZE_STRING); // OK

That part works just fine...

When I go to the URL with a username to the right of the / it displays the index page as it should and I have the username in the $ref variable.

Now I'm experiencing a different issue in the process, but will see if I can solve it myself before asking in here...

Thanks again for your response.

OK, good luck. :) Feel free to post your next question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.