Javascript inside PHP browser detection - php

Can anyone tell me why I keep getting the else and alert is not being triggered at all? The cookie is being set just while the browser is open.
<?php
$setcookie = setcookie('version', 'nova');
$browser = get_browser(null, true);
if(!isset($setcookie)){
if($browser["MSIE"] < 8.0){
// display message or alert!
echo "<script language=\"JavaScript\">\n";
echo 'alert("Please upgrade to version 8.0+ in order to view this site.");';
echo "</script>";
}
}
else
{
echo "Browser is current:";
}
?>

Your if condition is wrong. You want to check the $_COOKIE array, not the return value of setcookie.
if(!isset($_COOKIE['version'])) {
...
}

I don't think get_browser() returns what you think it returns? See the manual: http://php.net/manual/en/function.get-browser.php
You want to look at the [browser] and [version] fields.

PHP get_browser() method is very slow. Better use something like
$useragent = $_SERVER['HTTP_USER_AGENT'];
then you can do some preg_match as below
if(preg_match('/MSIE/i',$useragent)){
//echo something here
}
else{
//do something else
}

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"]);
}
?>

PHP app browser limit

I was found many class for detecting browser but I have problem with some of that, does anybody have easy source for this easy problem:
I want limit user who want work in one application only for Chrome and FF, I will make demo:
if($browser == 'Chrome' OR $browser == 'FF')
{
echo 'All ok';
}else
{
echo 'Not ok, I calling my internal redirection function and redirect user to applimit.php';
}
Use this, to know browser.
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
Reference php.net

Can I display all the cookies I set in PHP?

I am trying to diagnose an error in my cookies, but the names of the cookies are not what they should be. Is there a way in PHP to print all the cookies that have been set by my domain?
Have you tried:
print_r($_COOKIE)
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
<pre><?php print_r( $_COOKIE ); ?></pre> will do what you want. You might also try phpinfo().
echo $_COOKIE["cookie_name"]; // Print an individual cookie
print_r($_COOKIE); // Another way to debug/test is to view all cookies
You can display all cookies defined by running the following php function:
var_dump($_COOKIE);
if($_COOKIE) {
print_r($_COOKIE); //print all cookie
}
else
{
echo "COOKIE is not set";
}
As with any inputs, security practices should include filtering and validation. Since all cookies are strings, sanitize the strings:
var_dump(filter_input_array(INPUT_COOKIE, FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY))
PHP Docs: https://www.php.net/manual/en/function.filter-input-array.php
if($_COOKIE) {
foreach ($_COOKIE as $key=>$val)
{
echo $key.' is '.$val."<br>\n";
}
}
else
{
echo "No Cookies are Set";
}
This will check if any cookies are set, if found will iterate through each and print out the cookie name and value

How to check if an include() returned anything?

Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}

javascript redirecting to main window doesn't take into account http option (&)

i'm triying to use javascript redirect to main window after log in succeed in an iframe.
this is my code :
if ($_GET['redirect']!='') {
$redirect=$_GET['redirect'];
$smart->assign('redirect',$redirect);
}
$redirect=$_GET['redirect'];
echo $redirect;
if(isset ($_SESSION['user'])&&$_SESSION['user']!='') {
$user->email=$_SESSION['user'];
$user->addCorporate();
$user->signIn();
$user->loadSession();
echo("<script language=\"javascript\" type=\"text/javascript\">");
echo "document.write('redirecting...');";
if ($redirect!='') {
echo 'self.parent.location = "'.$redirect.'"';
} else
echo 'self.parent.location = "index.php"';
//echo $redirect;
// redirect($redirect);
echo "</script>";
}
the echo $redirect displays http://xxxxxxxx/play.php?action=play&id=d59541b89828da34e9a8345a1bdafe2b
but the redirection is made to http://xxxxxxxx/play.php? (without the php option)
This sounds pretty mysterious.
Here's how I'd proceed: Turn off JavaScript in your browser and examine your created JavaScript. Then, at least, you know whether maybe for some bizarre reason the wrong URL is print out after all, or whether the problem's in the redirection part.
If you change the line:
echo 'self.parent.location = "'.$redirect.'"';
to this:
echo 'alert("'.$redirect.'")';
What happens?

Categories