SitePoint Sponsor |
|
User Tag List
Results 1 to 21 of 21
Thread: query based on url
-
Jul 15, 2001, 21:26 #1
- 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.
-
Jul 15, 2001, 22:09 #2
- 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?
-
Jul 15, 2001, 22:14 #3
- 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.
-
Jul 15, 2001, 23:26 #4
- 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;
}
-
Jul 15, 2001, 23:47 #5
- 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");
Thanks.
-
Jul 16, 2001, 00:31 #6
- 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.
-
Jul 16, 2001, 12:21 #7
- 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;
}
-
Jul 16, 2001, 13:09 #8
- 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.
-
Jul 16, 2001, 13:31 #9
- 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!
-
Jul 16, 2001, 16:29 #10
- 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";
}
-
Jul 16, 2001, 16:37 #11
- 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.
-
Jul 16, 2001, 16:46 #12
- 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);
-
Jul 16, 2001, 17:10 #13
- 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.
-
Jul 16, 2001, 18:05 #14
- Join Date
- Jul 2001
- Location
- Missouri
- Posts
- 3,428
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
good to hear. no problem.
-
Sep 10, 2001, 14:06 #15
- 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
-
Sep 10, 2001, 21:43 #16
Augh, regular expressions! I'd just use substr.
-
Sep 10, 2001, 21:59 #17
- 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);
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.
-
Sep 11, 2001, 13:26 #18
- 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!
-
Sep 11, 2001, 15:27 #19
- 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.
-
Sep 11, 2001, 18:00 #20
- 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!
-
Sep 11, 2001, 21:50 #21
- 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