Passing tracking variables with PHP echo? - javascript

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

Related

Conditional JS Redirect

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>
";
}

script redirect in php is recursive

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

display php file content in the javascript of another php file

So basically i want to html() a php file but i seem to fail, maybe it isn't possible but i want to make sure.
This is an example code (not the real one):
page.php:
<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<? include('php/content.php'); ?>");
</script>
php/content.php:
<div class='realContent'>Awesome Stuff Here</div>
Note: I know there is .load(), $.post and many others. I just want to know if this practic is possible, .html()-ing a php file's contents.
Thank you in advance, awesome community!
d3nm4k.
The code in my example does what you want.
Maybe you got the problem with the short opening tags that is < ? ? >.
The file I'm trying to load in my example is located in app/View/Tests/def.php and works just fine.
Hope it helped you!
<div class='content'></div>
<?php
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php
// Alternatively, you can try this one
//include $fileLocation;
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>
You can do it, the code you gave i tried it just by adding ?php
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<?php include('php/content.php'); ?>");
</script>
When the php is executed on server it replaces content with actual html inside html() function. And on client side javascript plays its role to render it.

META Tags "Refresh" Alternative - "<?php header("Location:.. ?>": Issues with php echo in php

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();
?>

Can PHP edit the javascript code before it executes

Is it possible to edit javascript code so that before it executes on page load I can inject few code changes through PHP. I want to use PHP so that changes happen on the server before page is loaded onto client side.
For example, this is what I have:
<script>
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&'>")
</script>
And this is what I want:
<script>
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&oneMoreParameter=paramValue'>")
</script>
I tried this with jquery but because this is flash, once it is loaded then adding parameter doesn't do anything. That's why I thought doing it in PHP because it executes on server side.
Any help on this will be great.
Thanks!
Take a look below:
<?php
$test = 'test';
?>
<script type="text/javascript">
var test = '<?php echo $test; ?>';
</script>
Hope, this will be helpful to you!
$ob = ob_start();
//EXECUTE YOUR CODE HERE, THE PAGE AND STUFF e.g. include('./file.html');
$pi = ob_get_contents();
ob_end_clean();
$js = <<<'EOD'
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&oneMoreParameter=paramValue'>");
EOD;
$pi = preg_replace("#video38.addParm(.*?);#s", $js, $pi);
echo $pi;

Categories

Resources