php preg_match multiple urls - php

I know next to nothing about php so this will make someone laugh probably.
I have this bit of code in index.php which checks a host header and redirects if a match is found.
if (!preg_match("/site1.net.nz/",$host)) {
header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm');
}
However, I need to make it check on potentially multiple sites. as follows.
if (!preg_match("/site1.net.nz/"|"/site2.net.nz",$host)) {
header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm');
}
This might actually be the correct syntax for all I know:-)

if (!preg_match("/(site1\.net\.nz|site2\.net\.nz|some\.other\.domain)/",$host)) {
header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm');
}

Try,
$hosts="/(site1\.com)|(site2\.com)/";
if (!preg_match($hosts,$host)) {
// do something.
}

// [12] to match 1 or 2
// also need to escape . for match real . otherwise . will match any char
if (!preg_match("/site[12]\.net\.nz/",$host)) {
header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm');
}
Or
if (!preg_match("/site1\.net\.nz|site2\.net\.nz/",$host)) {
header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm');
}

if (!preg_match("/(site1\.net\.nz|site2\.net\.nz)/",$host)) {
header('Location: http://www.siteblah.net.nz/temp_internet_block.cfm');
}
This would be correct RegEx syntax.
Let's say you have an array of urls.
$array = Array('site1.net.nz', 'site2.net.nz');
foreach($array as &$url) {
// we need to escape the url properly for the regular expression
// eg. 'site1.net.nz' -> 'site1\.net\.nz'
$url = preg_quote($url);
}
if (!preg_match("/(" . implode("|", $array) . ")/",$host)) {
header('Location: http://example.com/');
}

Related

PHP Harmful URL protection

I've made this script, but the 4th line isn't right and I have really no clue how to solve this. I really appriciate if someone helps me. This is my code:
<?php
$url = $_GET["url"];
$badsite = array("http://check.com", "http://hotmail.com");
if($url == $badsite) {
echo "This URL is harmful.";
} else {
echo "Not harmful";
header("Location: " . $_GET["url"]);
}
?>
So the thing which doesn't work is the following line
if($url == $badsite) {
How can I make it so it checks if the GET contains a $badsite?
You don't want to check if the value equals the array, you want to check if it's in the array. Perhaps something like this:
if (in_array($url, $badsite)) {
// ...
}
Side note, you don't need (or want, really) this echo statement:
echo "Not harmful";
header("Location: " . $_GET["url"]);
You might get an error by emitting output before sending a header. But even if you buffer output or in some other way suppress that error, there's no reason to emit output when returning a redirect response. The browser would display it for only an instant, if at all. A redirect by itself is a complete HTTP response, no output is required.
In this case you can use the function in_array:
http://php.net/manual/en/function.in-array.php
<?php
$url = $_GET["url"];
$badsite = array("http://check.com", "http://hotmail.com");
if(in_array($url, $basite)) {
echo "This URL is harmful.";
} else {
echo "Not harmful";
header("Location: " . $_GET["url"]);
}
?>

Check url for information with php

I am wanting to check the url of one of my pages if the user has landed correctly
for example if a user visits www.example.com/page.php?data=something
The user will beable to view the page
but if the user visits www.example.com/page.php he gets redirected
This is my current code
$checkurl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($check, "?data=")!==false){
}
else {
header("Location: index.php");;
}
I thought this would work and been at this for a while cant seem to see a problem but i am still learning...
You need to use $_GET
For example
if (!isset($_GET["data"])) {
header("Location: index.php");
}
PHP Manual $_GET
Have you tried
if(isset($_GET['data']))
{
}
else
{
header("Location: index.php");
}
This way you just check if there is a "data" on your URL
You can just use this since you are looking for a query string aka a $_GET request:
if ($_GET['data'] != 'something') {
header('Location: http://test.com');
exit();
}
Also, if you just want to check if they included ?data=:
if (!isset($_GET['data'])) {
header('Location: http://test.com');
exit();
}

preg_match: ensure the start and the end contains something

I want to have the regular expression that makes sure the beginning of the string contains 'http://' and '/' and the end.
This is a longer version I came up with,
if(!preg_match("/(^http:\/\//", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start."/>';
}
elseif (!preg_match("/\/$/", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link has ended with a /."/>';
}
but I thought these two expressions can put together like below, but it wont work,
if(!preg_match("/(^http:\/\/)&(\/$)/", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start and a / at the end."/>';
}
the multiple expressions that I try to combine must be wrong! any idea?
thanks,
Lau
if(preg_match('/^http:\/\/.*\/$/', $site_http))
{
...
}
The ^http:\/\/ forces http:// at the front, the \/$ forces a slash at the end, and .* allows everything (and possibly nothing) between.
For example:
<?php
foreach (array("http://site.com.invalid/", "http://") as $site_http) {
echo "$site_http - ";
if (preg_match('/^http:\/\/.*\/$/', $site_http)) {
echo "match\n";
}
else {
echo "no match\n";
}
}
?>
generates the following output:
http://site.com.invalid/ - match
http:// - no match

To manipulate URL based on coming URL by PHP

How can you manipulate the destination URL by the name of the starting url?
My URL is
www.example.com/index.php?ask_question
I do the following manipulation in the destination URL
if ($_GET['ask_question']) {
// Problem HERE, since if -clause is always false
if ( $login_cookie_original == $login_cookie )
{
include "/codes/handlers/handle_login_status.php";
header("Location: /codes/index.php?ask_question");
die("logged in - send your question now");
}
}
if (isset($_GET['ask_question'])) {
...
If you did a print_r() of $_GET you would see
Array
(
[ask_question] =>
)
which shows that ask_question is set, but is empty, so it tests false.
$location = "test.php";
if(isset($_SERVER['QUERY_STRING']))
{
header("Location:".$location . "?" . $_SERVER['QUERY_STRING']);
}
else
{
header("Location:".$location);
}
I think you want to replace that with:
if (isset($_GET['ask_question'])) {
Which will only be true if it's contained in the URL.
you could possibly check the $_SERVER['QUERY_STRING'] variable to see if it contains 'ask_question'
edit: fixed the typo
You can retrieve values after the question mark by using the $_GET super global. For the example ?ask_question=true.
//is ask_question true?
if($_GET['ask_question'] == 'true') {
echo 'ask_question is true';
} else {
echo 'ask_question is not true';
}
For variables without values (like ?hello), use $_GET in such way:
if(isset($_GET['hello'])) {
echo 'hello is there';
} else {
echo 'hello is not there';
}
You've asked a lot of very basic questions about PHP and you don't seem to have a grasp on how the language works. I suggest giving the documentation a good read before your next question.
PHP Documention Home
PHP: Variables From External Sources

[PHP]: Why page is not redirecting?

In the following code, the "header:" line is giving problem.
$q = mysql_query($a) or die(mysql_error());
$row = mysql_fetch_array($q);
$ValidationResponse = "false";
if ($_COOKIE['user_name'] != "")
{
while ($row) {
if ($_COOKIE['user_name'] = $row['username'])
{
$ValidationResponse = "true";
break;
}
}
if ($ValidationResponse == "true")
{
ob_start();
header("location:personal_view.php");
ob_clean();
}
else
echo "<script>alert('Invalid Login. Try Again.');</script>";
}
$_COOKIE['user_name'] = "";
Three useful functions I tend to have:
function redirect($url) {
while (ob_end_clean()) ; // do nothing
header("Location: " + $url);
exit;
}
function reload() {
redirect($_SERVER['REQUEST_URI']);
}
function reloadQS() {
redirect($_SERVER['REQUEST_URI'] + '?' + $_SERVER['QUERY_STRING']);
}
The above correctly handles what might be nested output buffers already but will fail if content has already been sent to the user, which you can't do anything about. I'd suggest using the above otherwise you'll litter your code with loops to clean buffers and there's no point in that.
You're using output buffering incorrectly, which is why it's failing. Change:
ob_start();
header("location:personal_view.php");
ob_clean();
to:
ob_end_clean();
header("Location: personal_view.php");
exit;
You should put the ob_start at the very beginning of the script
Also, i'm not sure about this, but i always seen the location header written in this way
header("Location: location.php");
Location with capital L an a space after the colon ": "
This might sound stupid, but are you sure you are not outputting anything before the header() function call? Apache won't redirect even if it finds a newline character before the starting tag <?php in a script.

Categories