i want to check remote url's page contents. IF remote site's page content contains string http://yahoo.com set $qqq = YH if not contains $qqq = NOYH. i am not talking about "url of that page" im talking about page content of url
$url = "'".$get['url']."'";
$needle = "http://yahoo.com/";
$contents = file_get_contents($url);
if(stripos($contents, $needle) !== false) {
$qqq = "YH";
}
But it's not working. Can anybody help me with the correct syntax? thanks..
$url = $get['url'];
$needle = "http://yahoo.com/";
$contents = file_get_contents($url);
if(stripos($contents, $needle) !== false) {
$qqq = "YH";
echo $qqq; // <--in order to echo, you need to call echo.
}
If your goal is just to echo YH if it exists, you can just call it directly with,
echo "YH";
Rather than storing it into a variable.
It think your code won't work. For a number of reasons:
In your first line, you create a string, that contains single-quotes. So basically, $url contains something like 'http://url.here'. If you pass this to file_get_contents you get an error:
$url = "'http://www.google.com'";
echo file_get_contents($url);
Warning: file_get_contents('http://www.google.com/'): failed to open stream:
No such file or directory in ...
You said want to check whether $url contains a certain string. But you are checking whether the document the URL is pointing to, contains this string.
3. Maybe you mean $_GET instead of $get to retrieve the parameter url that is contained in the URL?
Ok, I read from the comments that you indeed want to search for the string in the content. Still, the first line of code is wrong, so it is probably:
$needle = "http://yahoo.com/";
$contents = file_get_contents($get['url']);
if(stripos($contents, $needle) !== false) {
$qqq = "YH";
}
(<?= $qqq ?> should work as it is).
There seems to be some confusion with your question and the title.
To answer "if $url contains http://yahoo.com/" then the following will do:
$url = "'".$get['url']."'";
$needle = "http://yahoo.com/";
if(stripos($url, $needle) !== false) {
$qqq = "YH";
}
Of course, you can use <?=$qqq?> to output the result.
You need to debug, so break it down step by step:
<?PHP
// make sure you see any errors (remove this later)
error_reporting(E_ALL);
ini_set('display_errors',1);
$url = $get['url'];
die($url);
?>
Is the URL correct?
<?PHP
// make sure you see any errors (remove this later)
error_reporting(E_ALL);
ini_set('display_errors',1);
$url = $get['url'];
die(file_get_contents($url));
?>
Does your script echo what looks like the response from $url?
Then continue building out and testing...
Without seeing all of your code, nobody here will be able to guess what you're doing wrong, but it should be easy, fun, and instructional for you to figure it out for yourself.
I hope this answer sends you off in the right direction.
Make sure you have PHP warnings on -- you should always set error_reporting(E_ALL) in a development environment anyway.
Make sure you have allowed URIs as parameters for fopen based functions allow_url_fopen - http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Related
how can I write some preg_match() or any other condition to find whether the URL have "php" in the second place. Eg url below "http://a.com/php/abcpdf/".
If the URL have "/php/" in the second place my condition should execute else not.
You can use parse_url function to parse the URL data and process the URI path accordingly.
<?php
$url = 'http://a.com/php/abcpdf/';
$url_data = parse_url($url);
if(strpos(trim($url_data['path'],'/') . "/","php/") !== false){
echo "exists"; // your task
}
Demo: https://3v4l.org/XQlF1
this should match what you need
preg_match("#^/php/#", $_SERVER["REQUEST_URI"])
Could someone point out what I'm mistaking here? :)
<?php
$q = $_GET[q];
$acuman = <<<PARSE
input: (contains: "hello"){
output: "hello";
}
PARSE;
$acuman = str_replace("input: (contains: ", 'if(strpos(', $acuman);
$acuman = str_replace("){", ', $q) !== false) {', $acuman);
$acuman = str_replace("output: ", '$output = ', $acuman);
eval($acuman);
?>
I'm attempting to execute the string $acuman, a heredoc which has been altered by various str_replace functions. However, it is not doing what I intend it to do, and I am confused as of what to do as I've tried many different things.
Since many people seemed confused: My intention is for the code in the string $acuman to be executed as code properly. I just want the eval function to work. I know that eval is evil, please, stop: I'm just asking for help for solving the problem at hand.
Edit: When I echo the string $acuman, this is what I get:
if(strpos("hello", $q) !== false) { $output = "hello"; }
You have the arguments in the wrong order:
if(strpos($q, "hello") !== false) { $output = "hello"; }
strpos() takes the "haystack" (string being searched) as the first argument and the "needle" (string to find as within the "haystack") as the second argument.
Ok, so... $acuman appears to contain the following:
if(strpos("hello", $q) !== false) {
echo "hello";
}
Which indicates that $q needs to contain a portion of "hello" to echo the string "hello".
I don't see any problem here, EXCEPT that $q = $_GET[q]; won't work with any modern version because q is treated like a constant, not a variable nor a string literal array index. See this PHP documentation on the subject.
Upon changing to $q = $_GET['q']; instead (note the quotes), it seems like this code actually works. It will output "hello" whenever passing any portion of "hello" to the URL parameter (which gets passed to the PHP code).
Needless to say: Do not use this code for production. The code as it is is very vulnerable and allows a user to pass raw PHP code through to your script to execute. The function of this code can be completely re-written in a much safer manner, but you have expressed the desire to continue using eval(); so please be careful.
Enjoy.
How do I extract a location from a url using strpos? If I can't use strpos, then what is a good alternative?
Here's the URL:
http://www.kijiji.ca/rss-srp-jobs/ontario/c45l9004
I've tried to use strpos:
if (strpos($url,'ontario') !== false){
echo "yes";
}
But it comes up with nothing. On top of this, I need to determine the source of the feed. The breakdown's like this:
Determine the source of the RSS Feed (Kijiji)
Check for the location in the $url variable (ontario) only if the source is Kijiji.
Here's my code so far:
if (strpos($url,'kijiji') !== false){
if (strpos($url,'ontario') !== false){
echo "ontario";
}
}
Ideally, Id like this script to check for a location in many different urls, so I can pinpoint the location when displaying a Job Ad.
Use parse_url function in order to get all the infgo you need.
See parse_url
Your script works for me. Are you sure you have defined $url? Like:
$url = 'http://www.kijiji.ca/rss-srp-jobs/ontario/c45l9004';
if (strpos($url,'ontario') !== false){
echo "yes";
}
And indeed have a look at url_parse function, which is for this kind of things
means domain related locality/local jobs
$listedDomains["http://www.kijiji.ca/rss-srp-jobs/%loc%/c45l9004"] = "ontario";
$listedDomains["http://www.example.com/rss-srp-jobs/%loc%/c45l9004"]= "bangalore";
foreach($listedDomains as $keyUrl=>$keyLocation)
{
echo str_replace("%loc%",$keyLocation,$keyUrl);
echo "<br />";
}
I am suffering from a problem while i take the current url of the page and spliting them into parts and then checking for the index.php phrase.So far i have done this:
<?php
$domain=$_SERVER['REQUEST_URI'];
$values = parse_url($domains);
$path = explode('/',$values['path']);
if(array_search($path[2], "index.php"))
{
echo "hello";
}
?>
but its not working so guys help me out and thank you in advance coz i know i will be satisfied by your answers.
Try this:
$pathToFile = $_SERVER['PHP_SELF'];
$currentFilename = substr($pathToFile, strrpos($pathToFile, '/') + 1);
if($currentFilename == 'index.php')
{
echo 'This file is index.php!';
}
$_SERVER['PHP_SELF'] is the path to the current file on the local system. Since you don't care about the domain name or the query string, this is easier.
strrpos($pathToFile, '/') gets the index of the last occurrence of / in $pathToFile.
substr($pathToFile, strrpos($pathToFile, '/') + 1) get the portion of $pathToFile starting with the character after the index found by strrpos() in step 2.
You should be left with only the filename in $currentFilename, which you can compare with whatever you choose.
Note that this will match any index.php file, not just the one at your domain root. For example, if your site is located at http://example.com, http://example.com/subdir/index.php would also be true for $currentFilename == 'index.php'. If that's not what you want, you'd do it a little differently.
Use this:
$domain=$_SERVER['REQUEST_URI'];
$path = explode('/',$domain);
if(array_search($path[2], "index.php"))
{
echo "hello";
}
I'm not sure what parse_url() is, but it didn't seem to do anything in your code.
I trying to code a login script for phpmyadmin
<?php
$user = "Domain";
$passwords = file("passwords.txt");
foreach ( $passwords as $pass){
$source = file_get_contents("http://dbadmin.one.com/index.php?lang=en&server=1&pma_username=$user&pma_password=$pass");
if(preg_match("/Database/", $source)):
echo "Login Worked with: {$pass}";
endif;
}
?>
My Problem is it , it dont works here
echo "Login Worked with: {$pass}";
Can you see the problem?
Not necessarily the solution to your problem, but some basic error checking might point you in the right direction. Your problem may even begin at the initial call to file.
$passwords = file("passwords.txt");
if (!$passwords) {
echo 'Unable to read password file';
} //etc
$source = file_get_contents("http://dbadmin.one.com/index.php?lang=en&server=1&pma_username=$user&pma_password=$pass");
if (!$source) {
echo 'Unable to read file source';
} //etc
Also as a side note if you were calling this function on a file outside your filesystem wouldn't you only get the output (HTML) similar to calling it in your browser (not sure if that was your intention).
$source = file_get_contents("http://dbadmin.one.com/index.php?lang=en&server=1pma_username=$user&pma_password=$pass");
if(preg_match("/Database/", $source)):
The value of $source is going to be the full HTML response of getting the url:
http://dbadmin.one.com/index.php?lang=en&server=1&pma_username=$user&pma_password=$pass
preg_match is only going to match on the first line of that string. You will need to parse the string differently or replace any newline characters so it will match on the whole file.
It also looks like you're testing logins to a database using the username "Domain" with a number of different passwords. Not sure if that was your intention, but it seems a bit odd.