Условие php для ссылок
PHP if else Request URI help
to see the sample click here see the block where it says Our services in the bottom I don’t want it to be shown on ths page but visible to all other pages.
and why, can I ask, did you try this? have anyone on here suggested it? you’ve completely changed your answer, that’s not nice at all.
I have tried all the suggested answer here and it didn’t worked. I haven’t changed anything I just applied one of the suggestion here and to ask programmers who has more experienced than me.
9 Answers 9
Always indent your code — it’s simplier to see errors
whatever
"; > else < echo "do not show
"; >; ?> Note the placement of curly brackets.
I’m being pedantic here i think, but his syntax was fine except that he was missing the word echo directly after ‘else’. But yes syntax was weird, but legal I think?
Or you could use the ternary operator. All on one line if you like — I broke it up to avoid the evil scrollbars.
echo ($url == "http://www.sample.com/test.php") ? "Whatever
" : "";
You’re using the wrong values.
// REQUEST_URI is the requested URI path plus the requested URI query, so let’s strip the latter $_SERVER['REQUEST_URI_PATH'] = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']); // HTTP_HOST may not be set if the request didn’t contain the Host header field (just HTTP/1.0) if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']=='www.testground.idghosting.com' && $_SERVER['REQUEST_URI_PATH'] == '/idi/our-production/') < // host is "www.testground.idghosting.com" and requested URI path is "/idi/our-production/" >
See the manual for what values the $_SERVER contains.
whatever
"; > else < echo "
do not show
"; > ?>
Your syntax was just a little wrong.
There does not currently seem to be a direct way. What you’ll have to do is to reconstruct the URL by combining entries from the $_SERVER array. You can check the results of the phpinfo() to see what entries you need.
At a higher level, have you considered changing your approach for getting this onto the pages where you want it? You could put the section within the then part of your code into a separate file which you include into those files in which you want it to appear.
The problem here is that in the echo string, you open a php tag, but that is already php code, so you don’t have to open that tag. You’d have to one one if you were in HTML code.
Источник
check if a string is a URL [duplicate]
I’ve seen many questions but wasn’t able to understand how it works as I want a more simple case. If we have text, whatever it is, I’d like to check if it is a URL or not.
$text = "something.com"; //this is a url if (!IsUrl($text))< echo "No it is not url"; exit; // die well >else < echo "Yes it is url"; // my else codes goes >function IsUrl($url) < // . >
8 Answers 8
The code below worked for me:
if(filter_var($text, FILTER_VALIDATE_URL)) < echo "Yes it is url"; exit; // die well >else < echo "No it is not url"; // my else codes goes >
You can also specify RFC compliance and other requirements on the URL using flags. See PHP Validate Filters for more details.
your first line should be checking for false, so its actually if(!filter_var($text, FILTER_VALIDATE_URL)) or flip your if/else.
PHP’s filter_var function is what you need. Look for FILTER_VALIDATE_URL . You can also set flags to fine-tune your implementation.
No regex needed.
However this is only true if you use http in front of the URL. For instance, www.google.com will not be validated correctly.
Technically, according to the definition of a URL (or URI) it requires a protocol (http, https, etc) to be the first thing in the string. What you’re describing is just the domain and path portions of the full URL.
)"; // Host or IP $regex .= "(\:2)?"; // Port $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor if(preg_match("/^$regex$/i", $url)) // `i` flag for case-insensitive < return true; >?>
but your example URL is over simplified, (\w+)\.(\w+) would match it. somebody else mentioned filter_var which is simply a filter_var($url, FILTER_VALIDATE_URL) but it doesn’t seem to like non-ascii characters so, beware.
I would recommend looking into the GET Query. Didn’t parse a youtube link. I need to make some changes to make them work based on: stackoverflow.com/questions/23959352/…
Check if it is a valid url (example.com IS NOT a valid URL)
if(!isValidURL($fldbanner_url)) < $errMsg .= "* Please enter valid URL including http://
"; >
Regexes are a poor way to validate something as complex as a URL.
PHP’s filter_var() function offers a much more robust way to validate URLs. Plus, it’s faster, since it’s native code.
If you look above, the original question does not mention anything about PHP. If anything it’s asking how to achieve the outcome without the use of JS. So in actual fact, your answer is not appropriate for the question asked. Furthermore, the answer Jack approved should not have been given the green tick, as it’s a PHP answer, to a non-PHP question. Yet another example of poor moderation IMO.
The question is tagged with «php». The example code provided is PHP. The asker probably should have been more explicit with his question—but it’s clear from context that he’s trying to validate the URL server-side, and is using PHP as his server-side language. And I am now done with this silly argument 🙂
Источник
Best way to check if a URL is valid
I want to use PHP to check, if string stored in $myoutput variable contains a valid link syntax or is it just a normal text. The function or solution, that I’m looking for, should recognize all links formats including the ones with GET parameters. A solution, suggested on many sites, to actually query string (using CURL or file_get_contents() function) is not possible in my case and I would like to avoid it. I thought about regular expressions or another solution.
Using CURL or getting it’s HTTP contents may be slow, if you want something more speedy and almost as reliable, consider using gethostbyaddr() on the hostname. If it resolves to an IP, then it probably has a website. Of course this depends on your needs.
12 Answers 12
filter_var($url, FILTER_VALIDATE_URL);
Validates value as URL (according to » http://www.faqs.org/rfcs/rfc2396), optionally with required components. Beware a valid URL may not specify the HTTP protocol http:// so further validation may be required to determine the URL uses an expected protocol, e.g. ssh:// or mailto:. Note that the function will only find ASCII URLs to be valid; internationalized domain names (containing non-ASCII characters) will fail.
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE)
Be aware that FILTER_VALIDATE_URL will not validate the protocol of a url. So ssh:// , ftp:// etc will pass.
@JoshHabdas, I think you’re missing the point. The PHP code does exactly what it claims to do. But it can’t read your mind. There’s a huge difference between invalid and unwanted.. Unwanted is very subjective, which is why it’s left to the programmer to work out that detail. You might also note the code validates the URL, but doesn’t prove it exists. It’s not PHP’s fault that a user mistyped «amazon,» «amozon,» which would validate, but is still unwanted.
Here is the best tutorial I found over there:
FILTER_FLAG_SCHEME_REQUIRED - URL must be RFC compliant (like http://example) FILTER_FLAG_HOST_REQUIRED - URL must include host name (like http://www.example.com) FILTER_FLAG_PATH_REQUIRED - URL must have a path after the domain name (like www.example.com/example1/) FILTER_FLAG_QUERY_REQUIRED - URL must have a query string (like "example.php?name=Peter&age=37")
@ErichGarcía this code doesn’t check that it’s a valid HTTP/S URL’s like the OP asks. This will pass things like ssh://, ftp:// etc this only checks if its a syntactically valid URL according to RFC 2396
Do not use FILTER_VALIDATE_URL. It is messy and unreliable. E.g. it validates ttps://www.youtube.com as valid
Using filter_var() will fail for urls with non-ascii chars, e.g. (http://pt.wikipedia.org/wiki/Guimarães). The following function encode all non-ascii chars (e.g. http://pt.wikipedia.org/wiki/Guimar%C3%A3es) before calling filter_var().
// example if(!validate_url("http://somedomain.com/some/path/file1.jpg")) < echo "NOT A URL"; >else
This is not a check which will get 100% correct results! This will only handle non-ascii characters in the path, not in the domain path of the URL. Nowadays, you can also use other unicode chars in the domain — which will be converted to punycode (see en.wikipedia.org/wiki/Punycode), e.g. «guimarães.org». So if you regard the non-punycode converted URLs as valid — your check will fail on these. Even if you handle this in the check, there is still the question of e.g. «ttps://mydomain.org» being falsely interpreted as valid! (as pointed out in other answers)
function is_url($uri)< if(preg_match( '/^(http|https):\\/\\/[a-z0-9_]+([\\-\\.][a-z_0-9]+)*\\.[_a-z]'.'((:8)?\\/.*)?$/i' ,$uri)) < return $uri; >else < return false; >>
Personally I would like to use regular expression here. Bellow code perfectly worked for me.
$baseUrl = url('/'); // for my case https://www.xrepeater.com $posted_url = "home"; // Test with one by one /*$posted_url = "/home"; $posted_url = "xrepeater.com"; $posted_url = "www.xrepeater.com"; $posted_url = "http://www.xrepeater.com"; $posted_url = "https://www.xrepeater.com"; $posted_url = "https://xrepeater.com/services"; $posted_url = "xrepeater.dev/home/test"; $posted_url = "home/test";*/ $regularExpression = "((https?|ftp)\:\/\/)?"; // SCHEME Check $regularExpression .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass Check $regularExpression .= "([a-z0-9-.]*)\.([a-z])"; // Host or IP Check $regularExpression .= "(\:1)?"; // Port Check $regularExpression .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path Check $regularExpression .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query String Check $regularExpression .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor Check if(preg_match("/^$regularExpression$/i", $posted_url)) < if(preg_match("@^http|https://@i",$posted_url)) < $final_url = preg_replace("@(http://)+@i",'http://',$posted_url); // return "*** - ***Match : ".$final_url; >else < $final_url = 'http://'.$posted_url; // return "*** / ***Match : ".$final_url; >> else < if (substr($posted_url, 0, 1) === '/') < // return "*** / ***Not Match :".$final_url."
".$baseUrl.$posted_url; $final_url = $baseUrl.$posted_url; > else < // return "*** - ***Not Match :".$posted_url."
".$baseUrl."/".$posted_url; $final_url = $baseUrl."/".$final_url; > >
Источник
So I have a page title that is part of a Magento template; I’d like it to display 1 of 2 options, depending on what the URL is. If the URL is option 1, display headline 1. If the URL is anything else, display headline 2. This is what I came up with, but it’s making my page crash:
__('Create an account if you are a Post Graduate Endodontic Resident and receive our resident pricing. Please fill in all required fields. Thank you!') > else < echo $this->__('Create an Account') > ?>
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; if($host == 'http://domain.com/customer/account/create/?student=1')
What does «making my page crash» mean? Do you revieve an error message? Further on: Where did you define the variable $domain?
3 Answers 3
Are you looking for the URL that the page is currently on? You are using parse_url the wrong way; that is if you only want to get the host, or domain, i.e. only «dev.obtura.com». It looks like you want more than that. In addition, you are never setting the $domain variable, so parse_url() doesn’t know what to do with it. So as it is now, your if statement will always return ‘Create an account`.
Instead, set $host with $_SERVER variables:
$host = $_SERVER[‘SERVER_NAME’] . $_SERVER[‘REQUEST_URI’];
You will also need to remove the «http://» from your checking — $host will only contain everything after «http://»
As Aron Cederholm suggested, you need to add semicolons ( ; ) to the end of your echo statements.
So, your PHP code should look like this:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; if($host == 'domain.com/customer/account/create/?student=1') < echo $this->__('Create an account if you are a Post Graduate Endodontic Resident and receive our resident pricing. Please fill in all required fields. Thank you!'); > else < echo $this->__('Create an Account'); >
Источник