Sorry:- another rank new-be! My code is placed on both mobile and desktop pages so as to redirect to the relevant page. However this seems to cause a recursive recall with the web page trying to reload over and over. Do I need to exit; Any other options? As i said don't seem to be able to use headers.
<?php $useragent=$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(android...etc)
{echo '<script type="text/javascript">
window.location = "http://mobile.tebb.com.au/"
</script>';}
else
{echo '<script type="text/javascript">
window.location = "http://www.tebb.com.au/"
</script>'; }
?>
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(android...etc)
header ('http://example.com/');
?>
Try this
I find that the header method is way more efficient and reliable as opposed to doing window.location
Related
I am trying to protect my content from example.php file. Therefore, I need to disallow direct access to this page as well as any iframe point to it without my permission.
I configure a php file called iframe.php loading iframe from example.com. This is how I did:
Code for example.php:
<?php
session_start();
if(!isset($_SESSION['iframe']) || !isset($_GET['internal']) || $_SESSION['iframe'] != $_GET['internal'])
{
die("This page can be accessed just from within an iframe");
}
unset($_SESSION['iframe']);
?>
Code for iframe.php
<?php
session_start();
$_SESSION['iframe'] = md5(time()."random sentence");
?>
<iframe src="example.php?internal=<?php echo $_SESSION['iframe'];?>" width="500" height="100"></iframe>
It works well. However, When I click on any link in iframe.php, I will be redirected to other page. After that, I click the Back button in browser and receive the message: "This page can be accessed just from within an iframe".
Could you please help me keep the page working perfectly after getting back?
Thank you for reading.
Its Simple, you can do it by Javascript.
if(window==window.top) {
// not in an iframe
}
In your file with iframe code just set php cookies:
<?php
setcookie("stackoverflow", 1);
?>
In your php files add code:
<?php
if(isset($_COOKIE['stackoverflow'])){
echo "";
}
else{
die('Good Bye');
}
?>
Best Regards ;)
Here is the simple code I have placed before the on the Wordpress site:
<script type="text/javascript"> <!-- if (screen.width <= 800) {
window.location = "http://m.domain.com"; } //--> </script>
This issue is, this redirects all pages of our site to to the landing page. I only want the home page to be redirected. How would I write this?
Put this code in header.php of your theme.
<?php
if (is_front_page()) {
echo '<script type="text/javascript"> <!-- if (screen.width <= 800) {
window.location = "http://m.domain.com"; } //--> </script>';
}
?>
Redirects with javascript can cause your website to be penalized by Google.
You are much better off using a Header redirect with PHP. You can detect mobile users based on their user agent rather that relying solely on the width of the browser window.
If you can't write all the code yourself, this plug-in could be helpful for that.
https://wordpress.org/plugins/equivalent-mobile-redirect/
Put this code right at the top of your themes header.php
<?php
if(is_front_page())
header('Location: http://example.com');
?>
This will only redirect on the front page. Just replace example.com with your desired url.
To setup a redirect for a different but specific page, you can use get_the_ID().
e.g.
<?php
if(is_front_page())
header('Location: http://example.com');
elseif(get_the_ID() == PAGE ID HERE)
header('Location: http://example2.com');
elseif(get_the_ID() == PAGE ID HERE)
header('Location: http://example3.com');
?>
if you want to redirect on all pages but still have homepage go somewhere else and leave some pages alone, use the below code.
<?php
if(is_front_page())
header('Location: http://example.com');
elseif(get_the_ID() != PAGE ID HERE || get_the_ID() != 2ND PAGE ID HERE)
header('Location: http://example2.com');
?>
I am having a problem with php header redirect not working on safari
if (!isset($lang))
{
header('Location: http://domain.com/en/home');
}
I have read that this could be done with JS fairly simple, but no idea how to do it with the condition if $lang is set.
Should, I just execute the js within the "php if", or there is a better/clever way to do it.
Thanks.
First off, you should stop the php execution if you set a redirect header:
if (!isset($lang))
{
header('Location: http://domain.com/en/home');
exit;
}
Note: for this to work properly, you need to set this header before any other and before setting any doctype. Also, the exit prevents anything else from being sent to the broswer (content, other headers, etc). Mixing headers can result in unwanted behavior.
Secondly, you can do:
if (!isset($lang))
{
?>
<script>
window.location = "http://domain.com/en/home";
</script>
<?php
}
Using Java Script
if (!isset($lang))
{
?>
<script>
location.href = "http://domain.com/en/home";
</script>
<?php
}
You can try with ob_start()
ob_start()//Define at the top of the php page.
if (!isset($lang))
{
header('Location: http://domain.com/en/home');
exit;
}
You can use echo to print HTML tag <script> and put your JavaScript code inside this tag.
if (!isset($lang)){
echo "
<script>
window.location = "http://domain.com/en/home";
</script>
";
}
I'm working on figuring how to make this code work but just can't seem to crack it.
<?php
header("Location: http://domain-2.com?subid=<?php echo $_GET["subid"]; ?>" /><?php
die();
?>
I understand that it has something to do with the php echo inside the php, is there a way to fix this?
If none, are the below mentioned worthy contenders or is there something else that I haven't seen that would be the best choice.
The purpose of this is to redirect users to a different page.
The final URL would look like this:
http://domain-1.com/index.php?subid=subid
What this will do is that it will get the sub-id from the final URL and show stats on a tracker app.
By doing so, there will no longer be a need to change the sub-ids within the .php file.
I can simply add what I want to track outside the .php file & simply FTP just one file to any domain.
I know for sure this code works & is ideally what I'm looking for but,
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://example.com?subid=<?php echo $_GET["subid"]; ?>" />
</head>
</html>
I researched that meta refresh doesn't work on all browsers, which is not good, & that this code:
<html>
<head>
<script>setTimeout('top.location = \'http://example.com?subid=<?php echo $_GET["subid"]; ?>\'', 0000);</script>
</head>
</html>
Is one that I'm not sure will provide a great alternative "open for suggestions though",
it does work the way it's intended to be, but I'm not totally convinced this would be the next best option.
Much appreciated,
J.c.
You have syntax error correct code will be
<?php
header("Location: http://domain-2.com?subid=".$_GET["subid"]);
die();
?>
It should have to be like this
<?php
header("Location: http://domain-2.com?subid=".$_GET["subid"]." ");
die();
?>
I have a website that I'm sending traffic to but then geo-redirecting with the Maxmind javascript code.
When the user first arrives at the page there are tracking variables appended to the url... &c1=, &c2=, &c3=, etc...
Can someone help me grab the tracking info from the url and then php echo it in the redirect? Not sure exactly how this is done and I would appreciate the help. Here's what I have so far:
<script src="http://j.maxmind.com/app/geoip.js" charset="ISO-8859-1"
type="text/javascript"></script>
<script language="JavaScript">
var country= geoip_country_code();
if(country == "US")
{
<!--
window.location = "http://example.com/redirect/usa/index.htm?&c1=PHP ECHO HERE&c2=AND HERE
//-->
}
else
{
<!--
location.href
//-->
}
</script>
Pretty close, just need to open php
window.location = "http://example.com/redirect/usa/index.htm?&c1=<?php echo $_GET['c1']; ?>&c2=<?php echo $_GET['c2'];?>
You can also you the short hand for php echo <?= ?>
to get the variable from the url use the super global $_GET so one of the variable might be $_GET['c1'] check the manual for more information
If it's param in URL you can see :
$_SERVER['QUERY_STRING']
parse_url() can also be useful