John Davidson

php - Strrpos not working properly when URl exists

0 comments
Message:


I try to do something when the url contains a certain string. But this only works if the file is not avalaible.


example:


if (strrpos($_SERVER['REQUEST_URI'], '/file/1.txt') !== false) { 
header('HTTP/1.0 403 Forbidden');
}

Result: Is only working (=return 403) when 1.txt is non-existent


Is there anybody out there who could clear my brain?


Thanks!



My Answer:
It seems like the issue might be with the way you are checking for the presence of the string '/file/1.txt' in the $_SERVER['REQUEST_URI'] variable.

One thing you can try is to use the strpos function instead of strrpos. strpos will return the position of the first occurrence of the substring in the string, while strrpos will return the position of the last occurrence.

Here is an example of how you can modify your code:

php
if (strpos($_SERVER['REQUEST_URI'], '/file/1.txt') !== false) {
header('HTTP/1.0 403 Forbidden');
}


This should work regardless of whether the file '1.txt' exists or not.

If this still doesn't work, you may want to check the value of $_SERVER['REQUEST_URI'] to make sure it contains the expected value. You can do this by adding a line like this before your if statement:

php
echo $_SERVER['REQUEST_URI'];


This will help you verify that the value of $_SERVER['REQUEST_URI'] is what you expect it to be.

I hope this helps! Let me know if you have any other questions.

Rate this post

3 of 5 based on 8057 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us