I am trying to figure out a condition that if there isn't the arguments "views" or "ckeditor" in the url, execute an echo on the page. The code that I am using and is not working is this:
<pre>
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//if (!strpos($url,'views')) {
if ((!strpos($url,'views')) OR (!strpos($url,'ckeditor'))) {
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">var $j = jQuery.noConflict();</script>' }
else {
echo '';
}
?>
</pre>
What is wrong in my script?
strpos() returns FALSE if the string is not found, and 0 if it is found at the beginning. In this case, you're trying to check if the URL contains either of these strings. So, you can simply check if it returns FALSE:
if ((strpos($url,'views') === FALSE) && (strpos($url,'ckeditor') === FALSE)) {
The above if condition will evaluate to TRUE if the URL doesn't contain views and ckeditor strings. If you only want to check for the existence of either one of the strings, then you can change && to ||.
a return value of 0 from strpos is a truthy response, however you are not checking for that. You should use strpos($url,'views') !== false
shouldn't this part have a semicolon
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">var $j = jQuery.noConflict();</script>'
like this
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">var $j = jQuery.noConflict();</script>';
only thing at first glance i noticed
If you are passing them thru a GET parameter you better use either $_GET or $_REQUEST instead of checking the url.
But if that's not the case, you may do something like:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strrpos($url, "views") === false || strrpos($url, "ckeditor") === false) {
...
}
Note for the 3 equal signs
Related
I want to perform an action when the url is /bookings , but not /bookings/something-else
I tried this...
if ($_SERVER['REQUEST_URI'] == '/bookings') {
// do stuff....
}
But it fails when the user is on the /bookings page and searches, at which point queries are added to the url, e.g. /bookings?search=this
I have also tried this...
if (strpos($_SERVER['REQUEST_URI'],'/bookings') !== false && strpos($_SERVER['REQUEST_URI'],'/bookings/') == false ) {
// do stuff...
}
But this still executes on /bookings/some-thing and i cant figure out why?
You'll be better off using a dedicated method for URL parsing, rather than using string manipulation. PHP's parse_url function is perfect for this:
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($path === '/bookings') {
...
}
Try this condition. first will make exact string. while seocnd match with query sring
if ($_SERVER['REQUEST_URI'] == '/bookings' || strpos($_SERVER['REQUEST_URI'],'/bookings?') !== false) {
// do stuff....
}
OR use === while matching false/ otherwise 0 and false become equal
if (strpos($_SERVER['REQUEST_URI'],'/bookings') !== false && strpos($_SERVER['REQUEST_URI'],'/bookings/') === false ) {
I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'austin') !== false) {
echo 'Austin metro';
} else if (strpos($url,'georgetown') !== false) {
echo 'Georgetown';
}
The issue is that this will match a URL that has austin anywhere.
For example, the url for georgetown parameter is like this: www.domain.com/austin/georgetown/
So if url has austin/georgetown, would like to display georgetown option.
Any suggestions? Thanks!
Go like this:
$url = 'www.domain.com/austin/georgetown'; //output: georgetown
if (strpos($url,'georgetown') !== false && strpos($url,'austin') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'georgetown') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'austin') !== false)
{
echo 'austin';
}
first if condition is checking if austin and georgetown, both are there in the url, if yes georgetown will be printed. rest two conditions are just checking for austin and georgetown individually.
See hear Php fiddle -> https://eval.in/715874
Hope this helps.
This question already has answers here:
PHP Notice: Use of undefined constant [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to check that BOTH $_GET conditions are true before executing a redirect, but my code is only checking if the 2nd one is true.
This is what I have:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'this-is-an-example-post') !== false) {
if($_GET['utm_campaign']==testing123 && $_GET['utm_source']==testing456) {
function preserve_qs() {
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
return "";
}
return "?" . $_SERVER['QUERY_STRING'];
}
header("Location: http://example123.com/this-is-an-example-post" . preserve_qs());
exit;
}
}
What it's doing is that if I set utm_campaign to = testing123 by itself, it won't redirect. That's good, I want it to require both. If I set both utm_campaign to = testing123 and utm_source to = testing456 then it does redirect, good so far. Now if I set ONLY utm_source to = testing456 it ALSO redirects, which means it's only checking for the 2nd condition to be true, but I need both to be true or for the script to exit, and I can't seem to figure out why it's not working the way it should.
This is in a Wordpress header.php file, not sure if it makes a difference.
Use quotes arround string
if($_GET['utm_campaign']== 'testing123' && $_GET['utm_source']=='testing456') {
I think you should check if the parameters are set and format the code a bit. Also there is no need to declare the function preserve_qs().
if (strpos($url,'this-is-an-example-post') !== false) {
if(isset($_GET['utm_campaign']) && isset($_GET['utm_source']) && $_GET['utm_campaign']=='testing123' && $_GET['utm_source']=='testing456') {
$qs = "?" . $_SERVER['QUERY_STRING'];
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false)
$qs = "";
header("Location: http://example123.com/this-is-an-example-post" . $qs);
exit;
}
}
I have a little script witch fetches data from IMDB with omdbapi.
I've managed to get the data from the site, but when I try to check if the movie's poster is valid, it always returns false.
if(!$info['Poster'] == "N/A") {
$url = $info['Poster'];
$img = 'images/'.$info["imdbID"].'.jpg';
file_put_contents($img, file_get_contents($url));
echo 'Downloaded';
} else {
echo '!Downloaded';
$noCover = true;
}
The $info['Poster'] is containing data similar to this:
http://ia.media-imdb.com/images/M/MV5BMTM0MDgwNjMyMl5BMl5BanBnXkFtZTcwNTg3NzAzMw##._V1_SX300.jpg
It was working a while ago, but it somehow stopped...
Your if statement is written incorrectly. !$info['Poster'] means if $info['Poster'] is not true. If there is a value it will be translated to false as PHP's type juggling converts any non-empty string to true and the ! operator makes that false. false does not equal N/A as type juggling converts that to true (non-empty strings are always true). false is not equal to `true.
You mean to use != which means not equal to
if($info['Poster'] != "N/A") {
Just move the ! from your condition, And it should work as expected. You are asking in your condition if $info['Poster'] is false, and it wont be false because it will have a string value. So, you are comparing a boolean value with a string value, false will be always different to "N/A:
if($info['Poster'] !== "N/A") {
$url = $info['Poster'];
$img = 'images/'.$info["imdbID"].'.jpg';
file_put_contents($img, file_get_contents($url));
echo 'Downloaded';
} else {
echo '!Downloaded';
$noCover = true;
}
My main question is, why the below code prints out:
false
boolean value true
I would expect the variable "boolean" value is also false
I want to store some data in javascript and later use it in PHP, is it even possible?
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Storage test?</title>
</head>
<body>
<script type='text/javascript'>
localStorage.save = 'false';
</script>
<?php
$boolean = "<script type='text/javascript'>" .
"document.write(localStorage.save);".
"</script>";
echo $boolean;
if($boolean = 'true'){
echo "<p>boolean value true</p>";
} else {
echo "<p>boolean value false</p>";
}
?>
</body>
Like said, you're not comparing, but assigning because of the single equal sing = in the if statement.
Next to that you cannot directly read the localStorage from PHP. So even if you had a double equals == to compare, then it would still outout boolean value true.
That is because you put a string inside $Boolean:
$boolean = "<script type='text/javascript'>document.write(localStorage.save);</script?";
You're not evaluating any JavaScript code like that.
When a PHP variable contains something, wether it be a string or number etc. it will always evaluate to true inside an if statement. Unless the value is either false, 0 or null.
To compare a real Boolean value you have to use an explicit compare. You do that with three equal signs ===.
if ( $someBool === true )
{ // do stuff }
But no, you cannot directly get the localStorage value from JS to PHP. You'd need an Ajax call to pass it back to PHP. And I think that is what you're ultimately trying to do.
if($boolean = 'true'){ <-- this line
echo "<p>boolean value true</p>";
} else {
echo "<p>boolean value false</p>";
}
You are not comparing the $boolean variable with 'true', but assigning the value 'true'.
Try two equal signs.
I'm not even sure what you're doing is possible. But the equal sign is definately a problem.
I guess it's a typo in the if you make
if($boolean == 'true'){
....
}
One = is to assign, comparison is either == or ===.
This example displays "Hello":
<?php
function getData()
{
return 'Hello';
}
if ($data = getData()) {
echo $data;
}
This example displays "23":
<?php
function getData()
{
return 'Hello';
}
$data = getData()
if ($data === 'hello') {
echo 1;
}
if ($data == 'hello') {
echo 2;
}
if ($data === 'Hello') {
echo 3;
}
Using = will only assign $boolean with that value and will return true because it's not false, 0, or null. Try using == or ===. :)