Find the Page Urls - php

I have page and I don't show a specific block to a specific urls.
So my urls are www.example.com/products/product1.html, www.example.com/products/product2.html etc..
So what I want to do. I want to find all the urls that starts with www.example.com/products/ and in those urls, exclude the block.
So far my code for one url is:
<?php
$a = "www.example.com/products/product2";
$p = curPageURL(); ?>
<?php
if($a == $p ){
Dont show the block
?>
But I have 100 urls that I don't show the block.
Is there any change to do for all the urls without write 200 lines of code?

Use PHP's strpos
This function will check if the specified string exists in the URL and if it exists, do not show the block.
$findme = 'www.example.com/products/';
$pos = strpos($mystring, $findme);
if ($pos === FALSE) {
// SHOW BLOCK
}

Try with strpos()
$cururl= "http://.".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$pos = strpos($cururl, '/products/');
if ($pos !== FALSE) {
// products found do your stuff
}

I think you can do this with the in_array()-function like this:
$blockedUrlArray[] = "www.example.com/products/product2";
$blockedUrlArray[] = "www.example.com/products/product3";
.
.
.
$searchUrl = curPageURL();
//if the current Url is not in blocked Urls
if(!in_array($searchUrl, $blockedUrlArray){
//do something
}
//if the url is a blocked url
else{
//do something
}

Related

How to simlify php code by using arrays from url strings

i'm new at this forum and php.
I want to show some info when the script detects one or more words in the postname (Wordpress)
In my exapmle like to dispaly extra info when Omnik + reset or wifi is detected.
I like to know how i can simplify the following code:
$url = "www.myurl.nl/postname"
if (strpos($url, 'omnik' )!==false){
echo "Omnik";
}
else if (strpos($url, 'reset' )!==false){
echo "Reset";
}
else if (strpos($url, 'wifi' )!==false){
echo "Wifi";
}
else {
echo "No Omnik,Reset or Wifi there";
}
At this moment i can only show the extra info when the word "Omnik" is detected.
Example: https://geaskb.nl/omnik shows the extra info, but https://geaskb.nl/omnik-reset and https://geaskb.nl/omnik-wifi should show the info too, while https://geaskb.nl/solaredge shouldn't show the info.
Hope you get what i mean.
====== Added 20:00 ========
Hi All, thanks for the ansewers.
I should have be clear the 1st time i guess.
This is the code i use now:
// Verkrijg URL incl. subdir.
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$geturl= "https";
else
$geturl = "http";
// Here append the common URL characters.
$geturl .= "://";
// Append the host(domain name, ip) to the URL.
$geturl .= $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$geturl .= $_SERVER['REQUEST_URI'];
if (strpos($_SERVER['REQUEST_URI'], "omnik" )!==false){
echo "Omnik in url";
}
else {
echo "Geen Omnik in $geturl";
}
============= 20:30u ==================
Problem solved!! stripos solved the problem!
Thanks for all your help!
One way for doing it through foreach loop
<?php
$url = "www.myurl.nl/postname";
$needles = ['omnik', 'reset', 'wifi']; // Add more if needed
foreach($needles as $needle){
if (strpos($url, $needle )!==false){
echo $needle;
}
}
?>

PHP conditional statement is not working

I have a statement that checks the page's url and marks up a page accordingly, but it only works when my if statement has one option to check for.
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
<?php if (strpos($url, 'events/eventname')!= false) { ?>
~markup~
<? } ?>
If I modify it to check for two possible urls...
<?php if (strpos($url, 'events/eventname')!= false) { ?>
~markup~
<? }else if (strpos($url, 'events/othereventname')!= false) { ?>
~markup~
<? } ?>
... the page won't load. I must be missing something obvious- can someone tell me what is wrong with this function?
*edit: Since it was requested I have included the $url variable and more specific url examples
strpos returns 0 when search substring is in the beginning of the query string. You can replace != to !== to make it work - otherwise php is internally transforming false to zero, which leads to incorrect comparison result.
For example:
<?php
var_dump(strpos('aaa', 'a'));
echo var_dump(strpos('aaa', 'a') === false);
echo var_dump(strpos('aaa', 'a') == false);
Try to use !== comparison just just in case string is at position 0.
Another syntax problem is else if, while you should use elseif.
Try also changing short php tag <? to full one <?php.
Rather than using the strpos() you can get the request uri which is anything after the domain name (ie: www.example.com/foo/bar would give you /foo/bar).
$url = $_SERVER['REQUEST_URI'];
if($url == "/foo/bar") {
// markup
} elseif($url == "/bar/foo") {
// markup
} else {
// markup
}

PHP Array to string conversion with preg_match

I have this error while I'm using this my script:
$pages = array('/about.php', '/');
//...............function text here................//
$ua = $_SERVER['HTTP_USER_AGENT'];
$mobiles = '/iphone|ipad|android|symbian|BlackBerry|HTC|iPod|IEMobile|Opera Mini|Opera Mobi|WinPhone7|Nokia|samsung|LG/i';
if (preg_match($mobiles, $ua)) {
$thispage = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
if ($thispage == $_SERVER["HTTP_HOST"].$pages) {
ob_start("text");
}
}
This script changes certain pages style depending on user's useragent. I need this script in such way. But I don't know how to make it in PHP properly. Maybe I need some "foreach ($pages as $i)"? But it didn't work in a way I made it.
You are trying to check if the "requested resource" $_SERVER["REQUEST_URI"] is in predefined list of resource paths.
Change your condition as shown below(using in_array function):
...
if (in_array($_SERVER["REQUEST_URI"], $pages)) {
ob_start("text");
}

Wordpress check url consists specific php file

I need to check in Wordpress admin page URL consists a specific php file. Suppose, I have an URL
http://localhost/candidate/wp-admin/edit.php?post_type=candidate-form
Now i would like to check if the edit.php exists in this URL.
Thanks in advance.
You could use strpos: http://php.net/manual/en/function.strpos.php
<?php
$url = 'http://localhost/candidate/wp-admin/edit.php?post_type=candidate-form';
$search = 'edit.php';
if (strpos($url, $search) !== false) {
echo 'found edit.php in url';
}

PHP url validation + detection

So here is what I need to do.
If an user enters this: http://site.com I need to remove http:// so the string will be site.com , if an user enters http://www.site.com I need to remove http://www. or if the user enters www.site.com I need to remove www. or he can also enter site.com it will be good as well.
I have a function here, but doesn't work how I want to, and I suck at regex.
preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_POST['link'])
Use filter_var() instead.
if (filter_var($_POST['link'], FILTER_VALIDATE_URL)) {
// valid URL
} else {
// not valid
}
There is also parse_url function.
I don't think I'd use regex for this, since you're only really checking for what is at the beginning of the string. So:
$link = $_POST['link'];
if (stripos($link, 'http://') === 0)
{
$link = substr($link, 7);
}
elseif (stripos($link, 'https://') === 0)
{
$link = substr($link, 8);
}
if (stripos($link, 'www.') === 0)
{
$link = substr($link, 4);
}
should take care of it.
i always go with str_replace haha
str_replace('http://','',str_replace('www.','',$url))
I think what you're looking for is a multi-stage preg_replace():
$tmp = strtolower($_POST['link']) ;
$tmp = preg_replace('/^http(s)?/', '', $tmp);
$domain = preg_replace('/^www./', '', $tmp) ;
This simplifies the required regex quite a bit too.

Categories