I've been using an .htacccess file to define a rewrite rule for extracting the username from the end of a url for years.

The username has always been a combination of letters and numbers following a forward / and that is all, and it has been working just fine. Here it is... When finished, $_GET['m'] would yield that username

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://domainname.com/load_error.php

Now I have an affiliate code from an outside source, so I can't control what the characters are in it.
I need to be able to capture the affiliate code at the end of a url that now begins with a ? and multiple characters after it.
examples ( ?afmc=1i / ?afmc=24 / ?afmc=2m / ?afmc=1b)

What I need is to capture the affiliate code including the ? to use for a DB query.

I don't know if the affiliate code will always maintain that exact structure as the number of affiliates increases beyond the possible number of combinations of 10 digits and 26 letters.

Any help would be appreciated to capture the ? and whatever comes after it.

thanks.
Douglas

jkon, thank you for that very thorough answer to the question.

I'm printing it out to study and figure out what piece or portion of that is relevent in my particular case.

Is there any chance you could point me in the right direction so I can get past this task and come back to do the studying afterwards?

If you could I'd appreciate it, but if not, I understand.

thanks again.
Douglas

Hello Douglas,

First of all let me thank you also because you read the answers , reply to those and you study them (that is the reason for writing those in first place, but sadly few people react the way you do)

Every portion of it, with that you will have the afterRootUrl so you can do what ever you want to it. If someone (or a robot) hits the “http://example.com/example/?afmc=1i / ?afmc=24 / ?afmc=2m / ?afmc=1b” (or even "http://example.com/example?afmc=1i / ?afmc=24 / ?afmc=2m / ?afmc=1b" )you will get the “?afmc=1i / ?afmc=24 / ?afmc=2m / ?afmc=1b” part as a string (even with those spaces) in the afterRootUrl. If you have it as a string you can validate it , split it or do what ever you like with it.

Great, thanks again for your response. Very valuable info to have.

My morning project...

Not to be redundant, but I have to say that is a great piece of code...

Once I read over it completely and understood what it was, it made me wish I would have had it a few years ago...

The one thing I haven't figured out how to extract from it, is the sub-domain if there is one.

I put the .htaccess on the base domain, and then in each sub-domain I have the index page, and one of the things that I need is the name of the subdomain as I use it in a DB table for visitor tracking purposes. If I can extract that from the URL, then I wouldn't need to name it in the script itself....

Not sure if that makes sense, but I know what I mean anyway... LOL

Is there a quick and easy function that would accomplish returning just the sub-domain name?

thanks in advance.
Douglas

Hello Douglas,
It is very difficult to built a generic method to get only the subdomain part because the way gTLDs are constructed. For example in http://example.co.uk there isn't any subdomain , but in http://eg.example.com the “eg” is the subdomain part. Its really easy if you know the domain though:

$domain = "example.com";
$subdomain = StringUtils->beforeFirst( $_SERVER["HTTP_HOST"] , "."  . $domain);

Here I used the StringUtils from the code snippet I linked but you got the idea and you can write it even without it.

Yep, it was just that simple...

$subdomain = StringUtils::beforeFirst( $httpHost ,"." ,$caseSensitive = false);
since the httpHost already contains sub.domain.com

That gave me exactly what I was looking for.

I'll bet with a little playing around I can find all sorts of ways to use this.

Thanks again
Douglas

jkon, please check your inbox...

thanks

jkon, if you are around could you check your inbox for me.

thanks
Douglas

This is an addendum to this thread because I've run into a separate issue while working through this process...

What I have is an affiliate link that is made up like this
http://domainname.com#AffiliateLink
or
http://domainname.com/#AffiliateLink

They both work perfectly fine for the affiliate software as far as knowing who the affiliate is, but I need to at the same time be able to extract what is after that # sign so I know what the affiliate link is and can search the DB for it.

That # seems to be the issue since it is an anchor tag.

I've located all sorts of articles sayint it can't be done, and some saying it can be done with javascript, which I don't understand... ex. http://stackoverflow.com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript

This is in a PHP script that we just switched affiliate scripts for.
The old one used a ? to indicate the affiliate ID but the new one uses the # which has made everything just that much harder.

Any suggestions would be greatly appreciated.

Thanks
Douglas

Hello Douglas ,
(I had daniweb open but I wasn't here) . The browsers never send what part comes after the pound symbol in a URL inside the request they made to the server, this is why there is no chance (in my knowledge) to get that part only server side (with any language). The pound symbol is used for inner / internal links inside the same html page so the browser don't even send them to server.

The only come around that is not actually a real solution is to do it server side + client side + server side again. What that means is:

  1. To generate a view server side and serve it to client having some sort of security token (yes , even using the session id is the most elementary method in that)

  2. Capture the URL , client side , and using AJAX post it to the server

  3. Server side: validate the request with some sort of validation (e.g. does the request have proper AJAX request headers? Do you have the visitor with that session id in that page where the request came from ? Or / And more advanced validations), take the URL and do whatever you like with it (even create a response with it , send it client side where with JS you will change the content) .

If any of these steps troubles you let us know which one and we will write code and ideas.
Bottom line using the pound symbol for passing data isn't a good idea exactly for that reason , because browsers don't even send the part after it to the server.

Kon

Thanks for your response jkon.

I am amazed at how many articles there are online that state specifically that you can do what I was trying to do and yet there are many others that give their version of how to make it work. I tried a few of them and couldn't .

Came to realize that I can't and why I can't, because the # and anything after it is ignored except for internal links, so there is no way to capture it.

But the good news is that I found a plug in for the affiliate system I'm using that will allow me access to the information I was trying to program to access. No extra work on my part.

Thank goodness... So, will close this ticket again and say thank you again for responding.

Douglas

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.