What I am trying to do is to display a message (1) if the current link does not contain the words "index" or "/?"
I found this to do the direct opposite:
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') !== false xor strpos($page, '/?') !== false) {
echo '1';
} else {
echo '2';
}
This code displays "2" on pages where there is no "index" or "/?" in the link, but I need the opposite: display "1" where there is no "index" or "/?" in the link.
BTW I have tried all combinations: !strpos, TRUE, !==, but it doesn't seem to work for me. I need a way without the "else" in the code, otherwise I could just change up the echos.
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') === false && strpos($page, '/?') === false)
{
echo"1";
}
else
{
echo"2";
}
Should display 1 if there's no index.php or /? in $page
The condition in your if statement is
if( <<COND 1>> xor <<COND 2>> )
where, xor's feature is
return TRUE if either <<COND 1>> or <<COND 2>> is TRUE, but not both.
You should instead use this:
if( strpos($page, 'index.php') !== false OR strpos($page, '/?') !== false )
Check this page for more information about logical operators.
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, '/?') === false && strpos($page, 'index.php') === false)
{
echo "1";
}
else
{
echo "2";
}
Try this :
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') === false || strpos($page, '/?') === false)
{
echo"1";
}
else
{
echo"2";
}
Related
function url(string $path = null): string
{
if (strpos($_SERVER['HTTP_HOST'], needle: "localhost")) {
if ($path) {
return CONF_URL_TEST . "/" . ($path[0] == "/" ? mb_substr($path, 1) : $path);
}
return CONF_URL_TEST;
}
if ($path) {
return CONF_URL_BASE . "/" . ($path[0] == "/" ? mb_substr($path, 1) : $path);
}
}
from what i saw this part of the code is not working
if (strpos($_SERVER['HTTP_HOST'], needle: "localhost"))
can someone give me a light please
strpos return an int so it can be 0. you can use
if (strpos($_SERVER['HTTP_HOST'], "localhost") > -1)
or
if (strpos($_SERVER['HTTP_HOST'], "localhost") !== false)
you can check for more : https://www.php.net/manual/en/function.strpos
This is probably going to have a simple answer, but here is my problem. I am currently writing a weak permissions filesystem, I want the user to not have to do any authentication checks if the given file is empty (this is the $filedir variable). I can successfully check if this file is empty, however, if I try to read anything else (shown by file_get_contents(data.data)), it just simply will not work. There are no errors, but the condition will always evaluate as true. I have been stuck on this forever, and I'm still new to PHP, so hopefully, someone can help me out here!
Thank you in advance!
Karl
<?php
$filedir = substr(getcwd(), 0, strpos(getcwd(), "this")).'this/is/' . $_SESSION['user_name'] . '/a' . '/' . $_POST['dataName'];
if ($_POST['c'] == "true") {
$filedir = substr(getcwd(), 0, strpos(getcwd(), "this")).'this/is/a' . '/' . $_POST['dataName'];
}elseif ($_POST['c'] == "") {
// code...
}else {
$filedir = substr(getcwd(), 0, strpos(getcwd(), "this")).'this/is' . '/' . $_POST['c'] . '/a' . '/' . $_POST['dataName'];
}
**//THIS IS THE FIRST CONDITION THAT, WHEN IMPLEMENTED, CAUSES THE SECOND CONDITION TO ALWAYS EVALUATE TO TRUE FOR SOME REASON**
$pass = false;
if (readfile($filedir) == 0) {
$pass = true;
echo "check";
}else {
echo "pass";
}
if ($_POST['auth'] == "1") {
$prev = getcwd();
chdir(substr(getcwd(), 0, strpos(getcwd(), "this")) . 'this/is/adir');
$cue = file_get_contents("data.data");
// throw new \Exception("Incorrect auth token", 1);
if ($_POST['token'] == $cue) {
$_SESSION['apiauth'] == $_POST['token'];
}elseif (file_get_contents($filedir) == '') {
$_SESSION['apiauth'] == '';
}else {
throw new \Exception("Incorrect auth token", 1);
}
chdir($prev);
}elseif ($_POST['permissions'] == true) {
addLog($fn,'Permissions were changed', 'DATABASE-PERMISSIONS', null, null, 'Target: '. $_POST['dataName'] . 'Change: {Type: '.$_POST['type'].', Usertype: '.$_POST['user'].', Name: '.$_POST['name']);
if ($_POST['revoke'] == true && ($_POST['user'] != 'u' || ($_POST['user'] == 'e' || $_POST['user'] == 'a' || $_POST['user'] == 'm' || $_POST['user'] == null))) {
throw new \Exception("Cannot revoke access without proper format", 1);
}
$prev = getcwd();
chdir(substr(getcwd(), 0, strpos(getcwd(), "this")) . 'this/is/adir');
$cue = file_get_contents("data.data");
**//BELOW THIS IS THE SECOND CONDITION THAT FAILS IF THE FIRST CONDITION IS IMPLEMENTED, AND WORKS FINE IF ITS LEFT OUT**
if ($cue === $_POST['token'] || $cue === $_SESSION['apiauth'] || $pass) {
if ($_POST['type'] == 'r') {
chdir(substr(getcwd(), 0, strpos(getcwd(), "this")) . 'this/is/a/dir/path');
if ($_POST['user'] == 'e' || $_POST['user'] == 'a' || $_POST['user'] == 'm') {
$cue = fopen($_POST['dataName'].".data", "w");
fwrite($cue, '**'.$_POST['user'].'**');
fclose($cue);
}elseif ($_POST['user'] == 'u') {
$d = file_get_contents($_POST['dataName'].".secure");
if ($d == '**a**' || $d == '**e**' || $d == '**m**') {
$cue = fopen($_POST['dataName'].".data", "w");
fwrite($cue, '');
fclose($cue);
}
if ($_POST['revoke'] == true) {
$writein = str_replace($_POST['name']."||","",file_get_contents($_POST['dataName'].".secure"));
$cue = fopen($_POST['dataName'].".data", "w");
fwrite($cue, $writein);
fclose($cue);
}else {
if (strpos(file_get_contents($_POST['dataName'].".secure"), $_POST['name']) !== false) {
// throw new \Exception("User already exists in permission slot", 1);
}else{
$cue = fopen($_POST['dataName'].".data", "a");
fwrite($cue, $_POST['name'].'||');
fclose($cue);
}
}
}else {
throw new \Exception("Invalid parameter.", 1);
}
}
}else {
addLog($fn,'Permission changed was blocked due to incorrect token', 'DATABASE-PERMISSIONS', 'danger', null, 'Target: '. $_POST['dataName'] . 'Change: {Type: '.$_POST['type'].', Usertype: '.$_POST['user'].', Name: '.$_POST['name']);
throw new \Exception("Incorrect auth token", 1);
}
chdir($prev);
}
?>
From the manual
Returns the number of bytes read from the file. If an error occurs,
FALSE is returned and unless the function was called as #readfile(),
an error message is printed.
You make a weak comparison on this line
if (readfile($filedir) == 0) {
}
If the call fails false == 0 will evaluate to true, because the int value will evaluate to false. false == false is true. So use strict comparison operator === and try to figure out why the call fails anyway.
if (readfile($filedir) === 0) {
}
or use, if intended if the call succeded, and returned anything (but also 0)
if (readfile($filedir) !== false) {
}
I have a recursive function
first of all one array of page URLs are passed to this function
then for each link and finds links in those pages, if those are pdf links then saved to $li array, if not then saved to $next array.
actually, in some pages, same links repeating.
so every time I want to pass $next array with new links only.but now $next array is storing all links and loop is not ending.please help
here is my code
<?php
global $next;
$next == array();
global $counter;
$counter = 2;
global $p;
$next is an array of page links got from a page
next_iter = get_html1($next);
function get_html1($next){
global $li;
global $all_links;
global $p;
$p++;
echo "count is" . $p . '<br>';
error_reporting(E_ERROR | E_PARSE);
global $URL;
global $counter;
$next = array_unique($next);
print_r($next);
foreach ($next as $nw) {
$page = '';
if ((strpos($nw, $URL) !== false ) && (strpos($nw, 'http://') !== false || strpos($nw, 'https://') !== false)) {
$page = $nw;
$html1 = file_get_html($page);
} else if (strpos($nw, $URL) === false && strpos($nw, 'http://') === false && strpos($nw, 'https://') === false) {
$page = $URL . $nw;
$html1 = file_get_html($page);
}
if ($html1 !== false) {
foreach ($html1->find('a') as $links) {
if (!in_array($links->href, $all_links)) {
if ($links->href != '#' && $links->href != NULL && $links->href != '/' && strpos($links->href, 'javascript') === false) {
$a = strip_tags($links->plaintext);
$a = preg_replace("/\s| /", '', $a);
$a = trim($a);
if ((preg_match('/.*\.pdf$/i', $links->href)) || (strcmp($a, 'Download') == 0) || stripos($a, 'Download') !== false) {
// echo $links->href;
if ((strpos($links->href, 'http://') !== false || strpos($links->href, 'https://') !== false)) {
$lm = $links->href;
if (!in_array($lm, $li)) {
write_to_folder($lm);
$li[] = $lm;
}
$li[] = $links->href;
} else if ((strpos($links->href, 'http://') === false && strpos($links->href, 'https://') === false)) {
// echo $URL . $links->href . '<br>';
$lm = $URL . $links->href;
if (!in_array($lm, $li)) {
write_to_folder($lm);
$li[] = $lm;
}
}
} else {
if (strpos($links->href, 'mailto:') === false && strpos($links->href, 'tel:') === false && strpos($links->href, '.zip') === false) {
if (!in_array($links->href, $next1)) {
$next[] = $links->href;
}
}
}
}
}
}
}
}
$next = array_unique($next);
echo "<br>next array";
if (!empty($next)) {
if ($p <= $counter) {
$recursiv = get_html1($next);
} else {
echo "SUCCESSFULLY EXECUTED";
exit;
}
} else {
echo "SUCCESSFULLY EXECUTED";
exit;
}
}
?>
I am trying to do if statement with OR but it wont work
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'website1.com','website2.com') {
die("...");
}else{
<content>
}
The above code says unexpected syntax ','
Another one I tried is
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'website1.com' OR $refData['host'] !== 'website2.com') {
die("...");
}else{
<content>
}
The above returns ... even though requested from the correct page
Also when there is no OR in the code with one it works fine but not when trying to add the second.
What am I doing wrong?
UPDATE: Couldn't get this working no reason why so just set CURLOPT_REFERER on website2 as website1 and and kept it as
if($refData['host'] !== 'website1') {
thank you everyone
Consider using in_array.
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
$array_data = ["website1.com","website2.com"];
if(in_array($refData, $array_data))
{
echo "Do Something!";
}
else
{
echo "Failed!";
}
Replace !== to != and use && or OR = || depending of your need
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] != 'website1.com' || $refData['host'] != 'website2.com') {
die("...");
}else{
echo 'hi testing';
//run your php code here
}
You problem is that you are checking if both are not identical
`!=` ( Not equal)
`!==` (Not identical)
&& example $x && $y True if both $x and $y are true
|| example $x || $y True if either $x or $y is true
||
Is the OR operator in PHP.
But here, you need an AND operator &&
Like this :
if($refData['host'] !== 'website1.com' && $refData['host'] !== 'website2.com')
I'm trying to add a trailing slash to urls using PHP. It can't be done with mod_rewrite since I have something similar to this in .htaccess:
RewriteRule ^page/(.*)$ index.php?page=$1 [L]
and I want to validate that the page exists before 301 redirect with trailing slash.
Right now I'm using this code after validation:
if(substr($_GET['page'], -1) !== '/')
header('Location: http://example.com/'.$_GET['page'].'/'.$_SERVER['QUERY_STRING'],TRUE,301);
But is there any better approach?
Simple way is that just remove the slash if available at the end of url and add it
$str = "http://yoursite.com/testpage";
OR
$str = "http://yoursite.com/testpage/";
echo rtrim($str,"/").'/';
You already have the best solution for this. I would just use $_SERVER['REQUEST_URI'] instead of the already parsed $_GET['page'] and $_SERVER['QUERY_STRING']:
if (substr($_GET['page'], -1) !== '/') {
$parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$uri = 'http://example.com'.$parts[0].'/'.(isset($parts[1]) ? '?'.$parts[1] : '');
header('Location: '.$uri, true, 301);
exit;
}
Here is my universal solution, hope it helps for somebody:
$site_adress = (((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
$whole_url = $site_adress . $_SERVER['REQUEST_URI'];
$pos = strpos($whole_url, "?");
$changed_url = FALSE;
if($pos !== FALSE && $whole_url[$pos - 1] != "/") {
$whole_url = substr_replace($whole_url, "/", $pos, 0);
$changed_url = TRUE;
} else if($pos == FALSE && substr($whole_url, -1) != '/') {
$whole_url = $whole_url . "/";
$changed_url = TRUE;
}
if($changed_url) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $whole_url);
exit();
}