SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query based on url

    I was wondering if its possible to make a query based on the url that someone accesses a shared database from.

    I'm working on a realestate listings database that is shared across multiple domains on the same server.

    Here's and example of what I'm currently using.
    agentwebsite.com/listings/index.php?agent=2

    and this is what I'd "like" to have -- just makes it look nicer.
    agentwebsite.com/listings

    I don't mind having ?agent=x in all the other URL's in the listings system but for advertising purposes, its nicer to see the main link without a query attached to it.

    Is something like this possible? Any suggestions are appreciated.

    Thanks.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a properly-configured server, if you go to /listings/, it'll be the same as going to /listings/index.php. But you do need to include the agent ID somehow.

    I may have misunderstood your question, but do you want to get the agent ID from the domain name? For example, do you want domain1.com/listings/ to be agent ID 1, and so on?

  3. #3
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, the server is set up so it will automatically look for the index file.

    Your right on your second assumption. I would like to get the ID based on the domain name.

    agent1.com/listings = agent1.com/listings/index.php?agent=1
    agent2.com/listings = agent2.com/listings/index.php?agent=2

    etc.

    Thanks for your time.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  4. #4
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    switch($HTTP_HOST) {
           case 
    'agent1.com':
                  
    $agent 1; break;
           case 
    'agent2.com':
                  
    $agent 2; break;
          default: 
    # if nothing else matches
                  
    $agent 3;

    So going to agent1.com/listings/ would set $agent to 1, agent2.com/listings/ would set it to 2, and so on.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do the domains need to be hard coded? There's a potential for about a hundred or so agents to be on this thing so adding each site to the .php file would take a bit of time.

    Each agent already has a bit of information in a table, if needed, I could add a field for their URL too.

    Would something like this work? If so, what else would I need to make it work like your example?

    PHP Code:
    (select id from agents where url ="$HTTP_HOST"); 
    I suppose its the same sort of idea as the spider friendly URL's.

    Thanks.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  6. #6
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, that would work. Then just set $agent to 'id' from the SQL statement.

  7. #7
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, I got it working --- mostly. It only works if $HTTP_HOST matches exactly what I have in the database. So if I have the www. in the db, and don't put that in the URL, it won't work. Is there any way that I can fix this?

    PHP Code:
    $headinfo mysql_query("SELECT id FROM agents where agenturl='$HTTP_HOST'",$link);
        if (!
    $headinfo) {
            echo(
    "<P>Error retrieving agent from database!<BR>""Error: " mysql_error());
            exit();
        }

        
    $headinfo_redirect = (mysql_fetch_array($headinfo));
        
    $id=$headinfo_redirect[id];

    if (
    $id) {
    $agent=$id;

    Thank you again.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2001
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try

    $headinfo = mysql_query("SELECT id FROM agents where agenturl like %$HTTP_HOST",$link);

    This would allow for differences at the biggining of http_host. But if http_ host happens to be 'abc.com' it would also return results for cdeabc.com.

    Or you could store all hosts as www.abc.com and the test to see if $http_host has www as the first 3 characters and if not then do

    $http_host = "www.".$http_host

    before doing the query.
    Marty H.

  9. #9
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would you go about evaluating $http_host to determine whether or not it has a www in it and if not, plugging it in.

    Thanks again for your help!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you want to add the "www." if it's not there right? this reg exp should do the trick:

    PHP Code:
    if (!preg_match('/^www\\./i'$http_host))
    {
        
    $http_host "www.$http_host";

    if you want to strip the "www." part if it exists (and just search for the domain), i can show you the reg exp for that if you want that.

  11. #11
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that would be great if you could show me how to strip out the www - I think it would be easier to store them without it.

    Thanks.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  12. #12
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k.

    PHP Code:
    $http_host preg_replace('/^(?:www\\.)?(.*)$/i''$1'$http_host); 
    if your PHP isn't at least version 4.0.4 you can't use that '$1' part. you'd need it to be '\1'. but '$1' is the preferred way.

  13. #13
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet! works perfectly. Thanks. phpinfo says that the server is running php v. 4.0.1pl2 so it looks like I'll have to upgrade soon.

    Thanks again.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  14. #14
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good to hear. no problem.

  15. #15
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Everything is working fine with the regex that you gave me a while ago but it seems that I need your help to take it just a bit further. I now need to remove http:// from addresses as well. Seems that some people on my site don't follow instructions so I have to make sure the url's are just the domain name using a more detailed regex.

    So if possible, I need urls like:

    http://www.domain.com
    http://domain.com

    to be

    domain.com

    The code that I am currently using is this:
    $newsletter = preg_replace('/^(?:www\.)?(.*)$/i', '\1', $newsletter);

    I tried to do it on my own but just end up getting confused.

    Thanks again for your time
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  16. #16
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Augh, regular expressions! I'd just use substr.

  17. #17
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh but del reg expressions are FUUUUUN man!

    jdulberg, this should work. i haven't tested it, but give it a try.

    PHP Code:
    $newsletter preg_replace('#^(?:https?://)?(?:www\\.)?(.*)$#i''$1'$newsletter); 
    that will handle "https" also. if you don't want that, take out the "s?". and don't let the pound signs (#) alarm you - i just use those as the pattern delimiter so i don't have to escape the "/" in http://.

    and again, since i guess your PHP version is < 4.0.4 replace the $1 with \1.

    let me know if it works for ya.

  18. #18
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, thanks for your code!! Its working perfectly. I took out the "s" from http as I'm pretty sure that the people using the site won't be linking to secure pages. Had to put the \1 as you suggested too as I only have php v. 4.0.1

    Thanks again for your time!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  19. #19
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem.

    if you took out the "s", did you take out the "?" too? because if you have it as "http?://" that will make the "p" optional. so in other words, you want this in the first set of parentheses: (?:http://)

    just makin' sure it's right for ya.

  20. #20
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah... good thing you told me Thanks for letting me know! So the final code is now:

    $newsletter = preg_replace('#^(?:http://)?(?:www\.)?(.*)$#i', '\1', $newsletter);

    Thanks again for your help!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  21. #21
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that code is correct. and, again, no problem.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •