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>
";
}
Related
is there any way I prevent direct access to my php gage but allow from javascript?
I have this code:
<script type="text/javascript" src="mypage.php?p=a"></script>
on mypage.php I have this code
<?php
Header("content-type: application/x-javascript");
if(!isset($_SERVER['HTTP_REFERER'])) {
header('HTTP/1.1 403 Forbidden');
exit;
}
else
{
$mycode="something here";
echo "document.write(\" $mycode \")";
}
?>
it is work fine if some one type url in browser, but if some one open mypage.php as a link
<a href=http://mywebsite.com/mypage.php>link</a>
, I want to prevent opening the page, is there any way to do that using php or javascript?
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 ;)
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
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
This is a theoretical question.
My question is whether a jQuery function or script can be written inside a PHP function.
E.g.
<?php function phpfunc(){
$a=10; ?>
<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>
Is this possible and legal?
Yes. It is possible and Legal one too. we generally use the same when we require any server side value to be set on client-side on runtime.
Hope this answers your query.
Thanks Much!
When php code is interpreted by the sever writing something like:
<?php
function foo()
{
<script type=text/javascript> ... </script>
}
?>
As part of the code in <?php ?> is interpreted as php and string inside the function doesnt represent any of php functions
You can echo javascript code (or any content of a HTML document) through your php code like:
<?php
function foo(){
echo "<script type=text/javascript> alert('it works!)'; </script>";
} ?>
so when you execute the function, you wil add the javascript to the document by echoing it and therefore execute it.
You can also use php variables to echo variables to javascript like:
<?php
function foo(){
echo "<script type=text/javascript> alert('{$phpVariable}'); </script>";
} ?>
or
<?php
function foo(){
echo "<script type=text/javascript> var variableFromPHP = {$phpVariable}; </script>";
} ?>
Yes, it's okay to do that. No, it's probably not a good idea. But there's nothing really stopping you.
Just be aware that if your variable happens to have a ' in it, you'll get messed up.
So, whenever you want to pass a variable from PHP to JavaScript, be sure to use json_encode:
<script type="text/javascript">
alert(<?php echo json_encode($something); ?>);
</script>
Note that there's no quotes in the JavaScript part - json_encode will add quotes if needed. This can be used to pass almost any kind of variable.
The short answer is no(edit : yes).
javascript is executed on the client and only on the client.
You can however echo javascript to the client.
So something like this :
$JSfunction = "<script>alert('This is working')</script>";
can be echoed to the page by doing echo $JSfunction;
edit :
Since you didn't mention where that function is located, I assumed you meant the PHP function on the server side.
To be clear, If it's written on the html page itself, it's perfectly fine and can be done.
complete answer
<? function phpfunc(){
$a=10; ?>
<script>var a ='<?php echo $a ?>'</SCRIPT> <?php } ?>
<?php phpfunc() ?>
<script>console.log(a);</script>
You must echo that $a
Yes you may use it like in normal html page. Use script include inside echo inside the php script.
You may use it outside the php script normally. Use container tag for the same in the php page.
I hope it would help.