PHP app browser limit - php

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

Related

PHP detect cookie value and redirect

I am trying to detect if a cookie named "authenticated" has a value of "true" if so redirect.
Here is what I have so far:
$named_cookie= "authenticated";
if($_COOKIE[$named_cookie] === "true"){
flush();
header( 'Location: https://app.website.com' );
exit (); }
For whatever reason it works in chrome but not in FF or IE
Try:
<?php
$named_cookie= "authenticated";
if(isset($_COOKIE[$named_cookie]) && $_COOKIE[$named_cookie] == "true"){
header('Location: https://app.website.com');
exit(); }
?>
;)
I figured it out. I am hosting my site on WPEngine and they dont allow cookie access with PHP it all has to be dont via AJAX and JS.

redirecting pages does not redirect when on mobile

Okay, so I have maconbeercompany.com which has an age verification redirect on it. It works great in IE, Chrome, Safari, and Firefox. Only problem is that I cannot get it to work with mobile devices. When on an iphone or android, I cannot leave the age verification page unless I hit 'no'. I want users to be able to click 'yes' and visit the beer company's website. Below is the code. What am I doing wrong?
<?php
// This code, including the enclosing ?php tags, will require age verification for this page if included at the beginning of the associated HTML/PHP file.
session_start();
if (!isset($_SESSION['age_verified']) || $_SESSION['age_verified'] != true)
{
// Direct them to the age verification page
$redirect_url = urlencode("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] );
header('Location: http://maconbeercompany.com/age-verification.php?redirect=' . $redirect_url );
}
?>
Thanks for your help!
Sometimes header doesn't work very well with sessions so i would recomend you try:
$redirect_url = urlencode("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] );
echo "<meta http-equiv=refresh content='0; url=http://maconbeercompany.com/age-verification.php?redirect=". $redirect_url."'>";
";
echo "window.open(``Location: http://maconbeercompany.com/age-verification.php?redirect=` . $redirect_url `, `_parent`, ``)";
echo " ";
}
?>

why is my header("Location: $_SERVER['HTTP_REFERER']"); PHP function not working?

It works when I input
header("Location: http://www.google.com");
but it doesn't work when I have
header("Location: $_SERVER['HTTP_REFERER']");
I want to redirect the page to whatever page it came from.
Try it: :)
if (!empty($_SERVER['HTTP_REFERER']))
header("Location: ".$_SERVER['HTTP_REFERER']);
else
echo "No referrer.";
However, for determining which page user came from, I'd rather use session variable, which gets reset at every page:
session_start();
echo "Previous page:", $_SESSION['loc'];
$_SESSION['loc']=$_SERVER['PHP_SELF'];
ps: This only works for local pages, you cannot track other websites.
You might try:
header("Location: {$_SERVER['HTTP_REFERER']}");
I've had problems with variable expressions which contain quotes in strings without braces.
You also need to look out for $_SERVER['HTTP_REFERER'] simply not being set. Some user agents don't set it, some privary tools mask it, and you need to handle people coming to your page without a referrer.
Here is a simple solution.
check and see what $_server['http_referer'] is giving you and if its set then you can redirect and if not put a fall back url something like :
if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != ""){
$url = $_SERVER['HTTP_REFERER'];
}else{
$url = "YOUR INDEX PAGE OR SOMETHING";
}
header("Location: ".$url);
This is a browser feature, and any polite browser will send the
correct header (although various 'security' tools will override this
with a fake referer).
It's browser specific so not every browser/security software combination will send it to the server. You're better off setting a session variable on each page load to determine which page the user came from (or something similar with a bit more logic)
header("Location: $_SERVER[HTTP_REFERER]");
Without the single quotes. This is the fastest way to access and concatenate array values without extra concatenating code.
Simply you can use
if(isset($_SERVER['HTTP_REFERER'])){
header("Location:".$_SERVER['HTTP_REFERER']."");
}
One of the mistakes that occure sometimes is, that NO OUTPUT must happen before header('Location: ' ....)
This is not working (shows the output, but doesn't redirect):
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
echo $res; // <--- OUTPUT COMES HERE
if ($res == true) header("Location: {$referer}");
}
This is working (does redirect properly):
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
//echo $res; // <--- NO OUTPUT COMES HERE
if ($res == true) header("Location: {$referer}");
}
This is also working, but doesn't make sense ():
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
if ($res == true) header("Location: {$referer}");
echo $res; // <--- OUTPUT COMES HERE, AFTER header('Location: ' ....)
}
(For better understandig, hope this may help)

Javascript inside PHP browser detection

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
}

PHP If Statement redirects on a die()

I am creating my own license checker since I often forget when to charge people for software every month. This is in infancy state.
I have an if statement checking a POST variable from another site before the site can run. (I know if the right person understood it he can manipulate it, but no such person!)
I have such statements working in the background, everytime a page loads thats part of the app. I cannot find a damn way around this though.
Here's the problem. When it successfully matches up and the variable returns yes, I have a die(). This takes the user off of the page they are working on, and redirects them to my checker script. I don't want that. If the variable returns yes, I just want the script to die (not do anything except stop), not redirect to a blank page because I put die.
I don't want the script to give a response, in other words.
Here's the snippet I am working with.
I want it to go back to the page it was on before. This script is an include, and the user should not know it's running. If the script completes, it takes the user to a blank page.
Here's the entire process.
License.php submits this form:
<form name="hidsys" method="post" action="../rauth/rauth.php">
<input type="hidden" name="siteid" value="<? echo $value; ?>">
<input type="hidden" name="keytype" value="a6request">
</form>
<script language="JavaScript" type="text/javascript">
setTimeout("document.hidsys.submit(hidsys)" ,1);
</script>
Here's my function and variables:
function rauth($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$timestamp = date("Y-m-d h:i", $timestamp + 10800);
$token = sha1($_POST['siteid']);
$rogue = sha1(time());
$authKey = md5($_POST['siteid']);
$skeletonKey = $token.$authKey;
$value = 'cca';
It talks to rauth.php:
<?php
// Declarations
$id = $_POST["siteid"];
$rauth_returned = rauth('http://www.mysite.com/rauth/index.php?siteid=' . $id . '&token=' . $token . '&time=' . $timestamp);
if (isset($_POST["siteid"])) {
if( strstr($rauth_returned, 'no')) {
exit('You are not authorized');
}
else if( !strstr($rauth_returned, 'yes')) {
exit('There was an error');
}
}
else {
exit("You can't view this page directly");
}
header('Location:' . $HTTP_REFERER);
?>
And this is the site index it talks to get if it's "authorized" or not:
<?php
if (isset($_GET["siteid"])){
$site = $_GET["siteid"];
switch ($site)
{
case cca:
echo 'yes';
die();
case ccgay:
echo 'no';
die();
default:
echo 'absno';
die();
}
}else{
echo "No data to process.";
die();
}
?>
Everytime a file (index.php) in a different direction than these files runs, it tells license.php to run in the background to simply check on if its allowed to run or not. Every link and every time the site opens it tells license.php to go through the process above. I probably wrote this really stupid but I just need something to help me remember when people don't pay I guess.
Instead of die() use header():
header('Location: http://www.example.com/');
UPDATED:
Try this:
posix_kill(posix_getpid(),9);

Categories