I have build URLs of my site like Stack Overflow URLs. My URLs are as below.
http://www.example.com/1255544/this-is-the-url-text
In this URL, 1255544
is the id and this-is-the-url-text
is the URL title text, both stored in the database. I have noticed that Stack Overflow URLs work on the base of id. Suppose here is a Stack Overflow URL
http://stackoverflow.com/questions/15679171/new-way-to-prevent-xss-attacks
In this URL, 15679171
is the id of the post. When I changed this id to 15706581
without touching the new-way-to-prevent-xss-attacks
and pressed Enter, the URL automatically changed to the following URL:
http://stackoverflow.com/questions/15706581/simplecursoradapter-does-not-load-
external-sqlite-database-id-error
And when I tried to remove some of the part of the URL title text like below
http://stackoverflow.com/questions/15679171/new-way-to-preve
It automatically corrects the URL as below:
http://stackoverflow.com/questions/15679171/new-way-to-prevent-xss-attacks
It means the data is being retrieved from the database on the basis of id 15679171
and the relevant URL title text, new-way-to-prevent-xss-attacks
, is attached according to the id.
I also want to do this. But the problem is, I am not going to understand that if someone change the id of the URL the title text should automatically changed. How do I do this?
I have thought the following:
$url_id = $_GET["id"];
$url_txt = $_GET["url_txt"];
$qry = "SELECT * FROM mytable WHERE url_id = '{$url_id}' LIMIT 1";
$data = mysql_query($qry);
$data_set = mysql_fetch_array($data);
if($url_txt== $data_set["url_txt"]) {
// proceed further
} else {
$rebuilt_url = "http://www.example.com/" . $url_id . "/" . $data_set["url_txt"];
header("Location: {$rebuilt_url}") // Redirect to this URL
}
Is it the proper and efficient way to perform this action or not? Is there a solution?