Reputation: 33
I'm a new CI developer dealing with security issues in one web site.
I have a url like:
http://myweburl/Everything/gov/2
which '2' indicates the parameter to query db.
Here is my question: If someone navigates following url:
http://myweburl/Everything/gov/2/order.txt
Actually nothing in the url, but..he will get an empty result page.(with reply 200 OK)
This is not what I wanted. I hope he'll get a 404 error or something else to tell users that nothing in the url.
Can anyone please give me some clues how to achieve my goal? Thanks a lot!
Upvotes: 1
Views: 469
Reputation: 14649
Basically you only want to show a valid page if their is the correct number URI segments in the URL that represents a parameter in the query.
You should check the number of URI segments
as a pre-process before calling any other methods. This way you can be sure that there is in fact only 1 segment in the URL
If you find that there exists more than 3 segments in your URL
, then handle that situation accordingly with the convenient functions that CodeIgniter offers such Error Handling.
You can check for the correct number of segments in the URL, and if it not what you want it to be, then call the show_404()
method.
In the example URL you posted:
http://myweburl/Everything/gov/2/order.txt
, there is actually 4 segments.
// from the docs http://codeigniter.com/user_guide/libraries/uri.html
$total_segments = $this->uri->total_segments();
// 3 segments would be the max if this is your url:
// http://myweburl/Everything/gov/2/order.txt (this url contains 4 segments)
// ^controller/function/uri_segment
// this means that there are 3 segments in your URL
// you don't want to have more than 3, so check for that
// with the total_segments() function
if($total_segments > 3)
{
$valid = false;
show_404();
}
else
{
$valid = true;
}
if($valid)
{
// process data as usual
}
Upvotes: 1