0

Im checking the "validity" (if is authorized to make something) of a domain trying to retrieve it from my database, the url came to my script over a GET var and then i just query the database:

$url = $_GET['url'];             // complete url of the site like: sub1.domain.com/page/1/some
$url = parse_url($_GET['url']);  // parsing the url http://php.net/manual/en/function.parse-url.php
$host = $url['host'];            // gives me sub1.domain.com 

After parsing:

$query = "SELECT * FROM domains_table WHERE domain = '" . $host . "'"; 

This works great, the think is sometimes domain.com have many sub domains and I cant save in database all the variations. It seems to me a lost of space if lets say domain.com have 30 sub domains and then I need to save sub1.domain.com sub2.domain.com and so...

Instead I was thinking in a more smart way, like saving in data base just *.domain.com and then somehow (and this is where i need help) in the query or in the php script (better if is in the query, personal taste) create something that tells * could be any string. How can i do this?

EDIT:

There is some confusion in the answers so I try to explain better my self:

If i have in my table saved on the url field: *.domain1.com how can i make something like this:

SELECT * FROM domain WHERE url LIKE "sub1.domain.com" 

To retrieve something.

4

5 回答 5

4

You want to use MySQL's Like operator.

Example:

SELECT * FROM table WHERE value LIKE '%string%';

The % matches any string before or after the "string" part.

于 2013-03-21T10:52:20.773 回答
2

您可以使用 % 作为星号(匹配之前或之后的任何内容)

$query = "SELECT * FROM domains_table WHERE domain LIKE '%" . $host . "%'"; 
于 2013-03-21T10:52:11.257 回答
0

you can use:

WHERE domain LIKE '%" . $host . "'"
于 2013-03-21T10:52:48.793 回答
0

不确定您是否可以在此处使用 LIKE,因为您的通配符将位于存储在表中的数据中,而不是您直接查找的数据中

更多的工作,但可能更好的是将域分成每个部分,然后在每个部分之间建立父子关系。

所以如果你有 shop.fred.com 和 news.fred.com,你就会有类似的东西。

id  parent  section
1   NULL    com
2   1   fred
3   2   shop
4   2   news

然后向下钻取。递归执行查询的简单粗略方式,或使用嵌套集合方法。

于 2013-03-21T11:22:03.110 回答
0

一种可能性是使用通配符选择器:

$query = "SELECT * FROM domains_table WHERE domain LIKE '%" . $host . "'"; 

有了这个,您可以匹配所有子域。所以随着

$host = '.domain.com';

您还可以检索所有子域。

但要小心,它很慢。

于 2013-03-21T10:53:20.727 回答