javascript redirection issue - javascript

i have 2 php files aff.php and goaff.php
in aff.php i execute some functions and then redirect the user to goaff.php
aff.php code
<?
here some php functions excuted
?>
<head>
<script type="text/javascript">
top.location.replace("http://www.example.com/goaff.php?id=text");
</script>
</head>
in goaff.php also i execute another php code and then redirect to website
goaff.php code
<?
here an php function excuted using id from aff.php then redirect
$url = "http://www.example.com";
header("Location: $url");
?>
my issue is with aff.php , with some users it doesn't redirect to goaff.php at all , i have test it by tracking the id from aff.php and log the results in a database , i need to know if there is something wrong with the redirection method in aff.php or not , i used javascript top.location.replace to prevent embedding the page inside frames and also to get sure javascript is enabled in the user browser but it logs more than 40% from the users are not redirected to goaff.php

I think Your problem is with redirection:
if that's the problem , use:
window.location.href = "http://www.example.com/goaff.php?id=text"
instead of
top.location.replace("http://www.example.com/goaff.php?id=text");

Related

prevent direct access to php page , but allow from javascript src

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?

Load php before html page load

I want to first load my php code to get some values ​​and then load my web html with the values ​​already loaded. This is possible? I am looking to do this, since I want to call my php code without using Ajax and at the same time the values ​​obtained by the Php will be loaded in my web html.
Like this:
Load www.example.com -> redirect to www.example.com/index.php
---index.php---
<?php
//do something
include("YourHtmlWeb.html");
echo '<script type="text/javascript">',
'YourFunction("hi");',
'</script>';
exit;
?>
---Html-javascript---
<script type="text/javascript">
function YourFunction(msg){
alert(msg);
}
</script>

How to allow my PHP file only load in an Iframe

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

No output to iframe after using window.location.href

I am using window.location.href in a php function which forces a file download without opening an additional window. The file download works and the rest of the php script executes. However, nothing that I output after the line with the javascript in the function will show up in the iframe. Is there a way to redirect the output back to the original php script?
Here is the javascript line from the php file:
echo "<script>window.location.href='download.php';</script>";
Here is the code from download.php:
<?php
session_start(); // Starts new or resumes existing session
$filename = $_SESSION['filename']; // Creates variable from session variable
header("Content-Type: text/plain"); // Output text file
header("Content-Disposition: attachment; filename=$filename"); // Output file name
readfile($filename); // Outputs the text file
?>
This is an example of something in the php file that will not output to the iframe after the line of javascript:
echo 'Test Output';
Thanks in advance,
Jay
EDIT
I replaced the line of javascript with the following and it works perfectly:
echo "<iframe id='my_iframe' style='display:none;' src='download.php'></iframe>";
You don't need to put the redirect inside the iframe page.
The best way I can think of atm is to use an invisible frame as mentioned in Andrew Dunn's answer to this question
Quoting from that answer:
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>
Have that inside your main page. url is basically the download url ("download.php" in your case). You can use a button to make the download manual. If you want it forced. Add src=url to the iframe and remove the script.
I would advice researching and using jQuery.

How to store the value returned by javascript function into a php variable?

The output generated by the program below is Inner textjain. Fine, but I want $m php variable to be assigned the value returned from JavaScript function a().
<html>
<head></head>
<body>
<?php
$name='abhi';
?>
<div id="a">
</div>
<script type="text/javascript">
function a(){
var a="jain";
<?php echo $GLOBALS['name'] ; ?>=a;
document.getElementById('a').innerHTML="Inner text"+<?php echo $name; ?>;
return <?php echo $GLOBALS['name'];?>;
}
</script>
<?php echo $n='<script type="text/javascript">' , 'a();' , '</script>';
echo $n;?>
</body>
</html>
You can't. Once the page is delivered to the browser, which is where Javascript runs, your PHP server has already shut the request down. If you somehow really want to send something calculated in Javascript back to PHP, you'll have to make an additional request to the browser from Javascript (using AJAX) but even then the variable will be available in a new request, not the old one.
PHP runs on the server, Javascript runs on the browser, and they don't run concurrently, so you'll have to send your Javascript variable to the PHP script (and re-run the PHP script), which involves reloading the page.
You can e.g. a GET request (by loading script.php?yourvariable=value).
you can't. PHP runs at the server first and then javascript will run at client side.

Categories

Resources