Wordpress check url consists specific php file - php

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';
}

Related

PHP Redirect to different URLs based on Parameters

I'm currently using this simple redirect (which passes all URL parameters to the next page) to redirect a link:
<?php
function preserve_qs() {
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
return "";
}
return "?" . $_SERVER['QUERY_STRING'];
}
header("Status: 301 Moved Permanently");
header("Location: https://example.com/" . preserve_qs());
?>
One of the issues is that I have with this method is that I need to create a separate file for each redirect.
Is it possible to make this into something that I simply add different URLs inside and based on URL parameter that I call, it sends people to the right URL.
For example, in PHP, we store 3 URLs and we assign them 3 values (parameters):
example1.com = page1
example2.com = page2
example3.com = page3
and the PHP file URL would look like this:
example.com/redirect.php?land=page1?restofparameters
keep in mind that the rest of the parameters need to be sent to the goal page, but not the page1 parameter which calls the URL inside the PHP file.
So the target URL that people will land will would look like this:
example1.com/?restofparamaters
Any help is appreciated. Thank you!
you can try .htaccess method... like this...
In .htaccess file
RewriteEngine On
RewriteRule ^test/(1)? http://triviamaker.com [R=301,L]
then for check this one
localhost/your_project_folder/test/1
Note:- that .htaccess file must be in Root directory of your Project.
& if you have any query or found any problem related to that .htaccess method then you can Search "redirect a specific url to another url with htaccess" in Google. you will Found more details easily about this...
hope this one is Helps to you... Thank You...
I would recommend a link parser.
But here goes an example based on your code for stripping and inheriting GET URL parameters.
<?php
function preserve_qs() {
if (empty($_GET)) return '';
return '?'.http_build_query(array_diff($_GET, ['land']));
}
http_response_code(301);
header('Location: https://example.com/' . preserve_qs());
exit;
?>
I write this in test.php ... just for your Reference
Run This File for check Output...
http://localhost/url_rewrite/test?2
and run also like this http://localhost/url_rewrite/test?land=2
echo $temp = $_GET['land'];
echo $temp = $_SERVER['QUERY_STRING'];
if($temp == "1" || $temp == "land=1"){
header('Location: https://example.com');
}else if($temp == "2" || $temp == "land=2"){
header('Location: https://gmail.com');
}else if($temp == "3" || $temp == "land=3"){
header('Location: https://apple.com');
}else if($temp == "4" || $temp == "land=4"){
header('Location: user/2');
}
I hope this helps to you... Thank you

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 code if part of url contains *this* then do *this*

I have a joomla site. I have an index.php that contains PHP code that displays divs + HTML code with divs
I have another index.php that should go on with /en and /fr versions.
and I have another version of index.php that I described above. I need to display one index.html on /ru version and another one on /en+/fr versions.
In other words, I need to echo some code in mysite.com/ru and another code on mysite.com/en + mysite.com/fr
<?php
$url = "http://mysite/en/";
$currentpage = $_SERVER['REQUEST_URI'];
if($url==$currentpage) {
echo 'index.html version one'
?>
But this didn't work.
Use strpos():-
if(strpos($url ,'en/') !== FALSE || strpos($url ,'fr/') !== FALSE) {
echo 'index.html version one';
}
if(strpos($url ,'ru/') !== FALSE){
echo 'index.html version two';
}
Example link:- https://eval.in/734505
Reference:-http://php.net/manual/en/function.strpos.php
Thank you again.
Here is my solution:-
<?php
$url = $_SERVER['REQUEST_URI'];
if(strpos($url ,'en/') !== FALSE || strpos($url ,'fr/') !== FALSE) { ?>
<div>my code for en+fr</div>
<?php } else { ?>
<div>my code for ru</div>
<?php } ?>

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
}

Find the Page Urls

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
}

Categories