Check url for if condition - php

So, I have the following condition for wordpress site:
if (is_page(array(1111, ???)) {
//do something for page# 1111 and url that contains "names"
}
I have a page that has url as following:
example.com/names/steve
example.com/names/mike
So, I want to check if the url is "names" as the condition.
Can someone tell me how to change the condition to check for "names" in the url?
Thanks
EDIT:
I changed to the following but no luck yet.
if ((is_page(1111)) || (preg_match("/\/names$/", $_SERVER['REQUEST_URI'])))
or
if ((is_page(1111)) || (basename($_SERVER['REQUEST_URI']) == 'names'))
or
if ((is_page(1111)) || (strpos($url, 'names') !== false))
any suggestions?

With preg_match() function using regular expression:
<?php
$url = 'example.com/names/steve';
echo (preg_match("/names/i", $url)) ? 'Match' : 'No Match';
?>
so for your example try this:
if (is_page(1111) || preg_match("/\/names\//", $_SERVER['REQUEST_URI'])){
// your logic
}

$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$myArray = explode("/", $link);
$page = end($myArray);
if($page == '1111') {
//Your code
}
Edit: In case URL contains trailing slash (or not)
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = rtrim('/', $link);
$myArray = explode("/", $actual_link);
$page = end($myArray);
if($page == '1111') {
//Your code
}

Related

Detect if URL equals to value and preforme action

I want to make action if the current url only equals to this: https://www.example.co.il/index.php?id=1000&2222
$url = 'https://www.example.co.il/index.php?id=1000';
if(strpos($url,'&2222'))
{
// Do something
echo "2222";
}
else
{
// Do Nothing
}
To exactly do what you are asked, try this
//actual link (http or https)
$actualUrl = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = 'https://www.example.co.il/index.php?id=1000';
if($actualUrl === $url) {
//do something
}
But if you just want to retrieve the id :
$id = $_GET('id');
//return 1000 in your case
You're able to read the parameters in the URL using the $_GET object. It lists the keys and values in the querystring, i.e. in your example,
https://www.example.co.il/index.php?id=1000
if you use:
print $_GET['id'];
you'll see 100.
so you could simply check for the existence of the key 2222:
if (isset($_GET['2222'])) { /** do something **/ }
bear in mind, this is only the case if you're actually reading a URL the script is running on.
your method of searching for a string within the URL is appropriate if you simply want to match a value in a string, whether its a URL or not.
USE THIS
// Assign your parameters here for restricted access
$valid_url = new stdClass();
$valid_url->scheme = 'https';
$valid_url->host = 'www.example.co.il';
$valid_url->ids = array(1000,2222);
$url = 'https://www.example.co.il/index.php?id=1000&2222';
$urlinfo = parse_url($url); // pass url here
$ids = [];
parse_str(str_replace('&', '&id1=', $urlinfo['query']), $ids);
if($urlinfo['scheme'] == $valid_url->scheme && $urlinfo['host'] == $valid_url->host && count(array_intersect($valid_url->ids, $ids)) == count($valid_url->ids)){
echo 'valid';
// Do something
}else{
echo 'in valid';
// error page
}

elseif not working with header location

I'm trying to get userclass to equal home when the user is on either usercp.php or profile.php, but I can't seem to get the following elseif to work:
if ($_SERVER['REQUEST_URI'] === '/usercp.php') {
$userclass = "home";
} elseif ($_SERVER['REQUEST_URI'] === '/profile.php') {
$userclass = "home";
} else {
$userclass = "norm";
}
I recommend you to try: echo $_SERVER['REQUEST_URI']; to see the value of the variable.
DonĀ“t forget that REQUEST_URI returns the path relative to the server root directory. So, if You have your php file inside a directory called "test", the string returned by REQUEST_URI will be "/test/thefile.php"
Try something like this ?
if (strpos($_SERVER['REQUEST_URI'], 'usercp.php')) {
$userclass = "home";
} elseif (strpos($_SERVER['REQUEST_URI'], 'profile.php')) {
$userclass = "home";
}
Or echo your $_SERVER['REQUEST_URI'] and make comparison like Rizier123 comment.

Get language of a website using simple html dom

I am building a search engine and webcrawler using PHP, and i would like to detect the language of a website, how would i detect the language of a page by:
Checking the URL for https://twitter.com/?lang=jap
if that is not set then i would like to:
Check the URL https://www.google.co.jp/
if i still can't find anything then i would to set default to English
the code i have so far for scraping pages is:
function crawl($url){
$html = file_get_html($url);
if($html && is_object($html) && isset($html->nodes)){
$weblinks[]=$url;
foreach($html->find('a') as $element) {
global $weblinks;
$link = $element->href;
$base_url = parse_url($url, PHP_URL_HOST);
if(substr($link,0,7)=="http://"){
$link = $link;
}else if(substr($link,0,8)=="https://"){
$link = $link;
}else if(substr($link,0,2)=="//"){
$link = substr($link, 2);
}else if(substr($link,0,1)=="#"){
$link = $html;
}else if(substr($link,0,7)=="mailto:"){
$link = "";
}else if(substr($link,0,11)=="javascript:"){
$link = "";
}else{
if(substr($link, 0, 1) != "/"){
$link = $base_url."/".$link;
}else{
$link = $base_url . $link;
}
}
if(substr($link, 0, 7) != "http://" && substr($link, 0, 8) != "https://" && $link != ""){
if(substr($url, 0, 8) == "https://"){
$link = "https://".$link;
}else{
$link = "http://".$link;
}
}
if(!in_array($link, $weblinks)){
$weblinks[]=$link;
}
}
$html->clear();
}else{
}
}
function info($weblinks){
foreach($weblinks as $link) {
$linkhtml = file_get_html("$link");
if($linkhtml && is_object($linkhtml) && isset($linkhtml->nodes)){
$titleraw = $linkhtml->find('title',0);
$title = $titleraw->innertext;
$des = $linkhtml->find("meta[name='description']",0)->content;
//detect language here
echo "<tr><td>".$title."</td><td>".$link."</td><td>".$des."</td></tr>";
$sql = mysql_query("INSERT into web once");
$title = "";
$des = "";
$linkhtml->clear();
}
}
}
To get the language from ?lang=:
$url = 'www.domain.org?lang=IT';
$url_parts = parse_url($url);
$lang = parse_str($url_parts['lang']);
You should then validate this with a switch/case statement and a list of languages that you support, like this:
switch ($lang) {
case 'EN':
//language is English
break;
case 'IT':
//language is Italian
break;
case 'FR':
//language is French
break;
default:
//?lang query was empty, or contained an unsupported language
$lang = FALSE;
} //end switch
After that, you can use this logic to determine whether you need to check the URL for the language:
if ($lang == FALSE) {
//code to determine language from TLD
}
Hopefully this will help get you started, although this is a big can of worms you're opening up. There are other things you need to check in order to be certain of the language of a website in addition to what you've mentioned. One of them is the language meta tag, which is like this: <meta name="language" content="english"> and goes in the head of the webpage, though not all websites use it.
Some multilingual websites, like mine, use a subdomain like http://it.website.com or http://fr.website.com
Others use query strings that are different from ?lang=. So you'll need to do a significant amount of research to cover all your bases.

If current URL equals rediectURL do nothing

Hey guys I am trying to build a redirect script in my header.
It contains a variable called $redirect that either equals 0 or 1.
What I want to do is if the variable equals 1 to redirect the user to a specified page.
That works.
The problem I am having is when it reaches the redirected URL it creates a loop.
I tried writing the following code but it does not work. What have I done wrong?
<?php
$redirect = 1;
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
$redirectURL = '/protest/cispa.php';
if ( $redirect === 1 ) {
if ( $url === $redirectURL ) {
die();
}
else {
header("Location: $redirectURL");
exit;
}}
?>
As suggested by andrewsi I updated my code to the following at it works:
<?php
$redirect = 0;
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = $self;
$redirectURL = '/protest/cispa.php';
if ( $redirect === 1 ) {
if ( $url === $redirectURL ) {
}
else {
header("Location: $redirectURL");
exit;
}
}
?>
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
$redirectURL = '/protest/cispa.php';
Your $url contains a fully qualified domain name; the redirectURL is just a path. The two are never going to be equal. Try setting :
$url = $self;
(If I'm reading your code correctly)
You could stick a flag in the query string of your redirect. Then, if the flag is present, don't add the redirect.
ex;
http://www.yoursite.com/redirectedto.php?red=1
Now, if red is set, I would not add the redirect.
lee

check my links format with php

I have something like this :
<?php
$siteurl = "http://www.example.com/";
$pageid = "ed2689";
$pageurl = $siteurl.$pageid;
?>
and the links will be like this:
http://www.example.com/ed2689
http://www.example.com/report/ed2689
I have used preg_match to check the format for each one of this links
for the first link, it must be exactly like this:
$siteurl.[a-z0-9]
and i have used this :
if (preg_match('/[$siteurl]+[a-z0-9]/', $pageurl) && !preg_match('/[=]|[?]/', $pageurl))
{
echo 'Ok',
}
and for for the second link, it must be exactly like this:
$siteurl.'report/'.[a-z0-9]
and i have used this :
if (preg_match('/[$siteurl]+[report]+[a-z0-9]/', $req_uri) && !preg_match('/[=]|[?]/', $req_uri))
{
echo 'Ok',
}
and it doesn't work correctly..any help please ?
Thanks.
Let me know if not works
<?php
$siteurl = 'http://www.example.com/report/ed2689';
//$siteurl = 'http://www.example.com/ed2689';
if(strpos($siteurl,'www.example.com') === false) echo 'not my site';
else {
$arr = explode('www.example.com/',$siteurl);
$suburl = trim($arr[1]);
if (preg_match('/^[a-z0-9]+$/', $suburl) || preg_match('/^report\/[a-z0-9]+$/', $suburl))
{
echo 'ok';
}
}
?>

Categories