I created a bit of code that just condition checks, like below
if ( $pretty_link_url==$_SERVER['HTTP_REFERER']) {
If this condition is true and working, then someone is using their browser with the same URL which is in "$pretty_link_url". "$pretty_link_url" is found in the database, and the if condition runs if they match. Now my question is
Is there anyway to get the result where both url from above $pretty_link_url==$_SERVER['HTTP_REFERER'] the same same as ...
$url = "result";
I don't know if it possible thankyou
I'm not sure what you really want but from your comment I think your looking for:
<?PHP
if ( $pretty_link_url == $_SERVER['HTTP_REFERER']) {
$url = $_SERVER['HTTP_REFERER'];
}else{
$url = ""; // empty or some other value you want to use instead
}
?>
I'm taking a guess at what exactly you're looking for, but is this it?:
$url = '';
if ( $pretty_link_url==$_SERVER['HTTP_REFERER']) {
$url = $_SERVER['HTTP_REFERER'];
}
Related
I think my question is some stupid because the code work, I only want know if I can do it better hehe, I want get a text from a URL usin GET, but I want check if it is empty, check if the text have numbers, and if the text is not a option of two options required condition_text_1 and condition_text_2, so I need do it with conditionals (IF), I will show my code:
if (isset($_GET['TEXT'])&&!empty($_GET['TEXT'])) {
if (!preg_match("/^[A-Za-z]+$/", $_GET['TEXT'])) {
$url = "DEFAULT_TEXT";
}else{
if ($_GET['TEXT'] !== 'condition_text_1' && $_GET['TEXT'] !== 'condition_text_2') {
$url = "DEFAULT_TEXT";
}else{
$url = $_GET['TEXT'];
}
}
}else{
$url = "DEFAULT_TEXT";
}
I dont know if its explained correct, but Im not talk english and Im new on php so it is a explosive post. Advanced thanks!!
Scenario: As you know, StackOverflow checks the title in the question. I mean when you open this URL:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function
automatically it will be replaced with this:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function-into-an-array
That replacement is because of being incomplete the first URL.
Ok well, I'm trying to make such a system by PHP. Here is my attempt:
// getting this from the URL
$id = '38839016';
// fetching this from database based on that id (38839016)
$real_title = $result['title'];
//=> Should I store the result of an function into an array
$real_title = str_replace("-"," ",strtolower($real_title));
//=> should-i-store-the-result-of-an-function-into-an-array
// getting whole uri
$current_uri = $_SERVER["REQUEST_URI"];
//=> /questions/38839016/should-i-store-the-result-of-an-function
What I want to do: I need to compare $real_title with title-part of the $current_uri. How can I determine "title-part"? It is everything which is after $id.'/' until / or ? or $ (end of string). How can I do that comparison?
And then:
if ( preg_match("//", $current_uri) ) {
// all fine. continue loading the page
continue;
} else {
// replace title-part of the $current_uri with $real_title
preg_replace("//", $real_title, $current_uri);
// redirect to this post with correct slug
header('Location: '.$_SERVER["HTTP_HOST"].);
}
briefly, I want to complete these:
if ( preg_match("//", $current_uri) ) {
preg_replace("//", $real_title, $current_uri);
Ok, in simple words, there is a good url and a requested url
if the requested url not equal the good url
redirect the visitor to the good one
<?
$id = '38839016';
$good_url = '/questions/'.$id.'/'.str_replace("-"," ",strtolower($result['title']));
preg_match( '/\/[0-9]+\/([a-z0-9-]+)\/.*/', $_SERVER[REQUEST_URI], $matches);
if ($matches[1] != str_replace("-"," ",strtolower($result['title'])))
header('Location: '.$good_url);
I'm building a php system which allow users to resend their invoices that need to be rechecked,
My problem is i need to make a condition to my system so it can't accept any url expect urls that looks like the following example
$url = http://mydomain.com/$id/invoice/$num
http://mydomain.com/murad/invoice/6589445564555
http://mydomain.com/ludmilla/invoice/9764564252
code:
} elseif(preg_match("|^http(s)?://(www.)?mydomain.com/([a-z]+)/(.*)?$|i", $url)){
$msg = '<div class="msg"><div class="error">'.$lang['er_01'].'</div></div>';
but it didn't work , Can i know how to make it work correctly ?
AFAIK, you need to reverse the test and change a bit the regex, try this:
} elseif( ! preg_match("|^http(s)?://(www.)?mydomain.com/([a-z]+)/invoice/(\d+)$|i", $url) ){
$msg = '<div class="msg"><div class="error">'.$lang['er_01'].'</div></div>';
I'm trying to make links to include the current _GET variables.
Example link: Page 2
The current url is: http://example.com/test.php?id=2&a=1
So if someone clicks on the link of page 2 it will take them to:
http://example.com/test.php?id=2&a=1&page=2
Currently if they click on the link it takes them to:
http://example.com/test.php?page=2
As you can see, I need a way to get the current _GET variables in the url and add them to the link. Advice?
The superglobal entry $_SERVER['QUERY_STRING'] has the query string in it. You could just append that to any further links.
update: The alternate response on this page using http_build_query is better because it lets you add new variables to the query string without worrying about extraneous ?s and such. But I'll leave this here because I wanted to mention that you can access the literal query string that's in the current address.
$new_query_string = http_build_query(array_merge($_GET,array('page' => 2)));
Make use of #extract($_GET). So you can access them directly as variables.
Try this may help you......
function get_all_get()
{
$output = "?";
$firstRun = true;
foreach($_GET as $key=>$val) {
if($key != $parameter) {
if(!$firstRun) {
$output .= "&";
} else {
$firstRun = false;
}
$output .= $key."=".$val;
}
}
return $output;
}
As for the question above how to include the name of php file as well in the url, using Your Common Sense's perfect method and adding a question mark worked for me:
echo "<a href='?".$url."'>link</a>"
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