PHP "header (location)" inside IFRAME, to load in _top location? - php

I have a simple form which is inside IFRAME. When user click on SUBMIT, it redirects to a specific page on my server. The function I use for the redirect is
header ('Location: mypage2.html');
exit ();
But I want the new page to open in _top location, not inside the same IFRAME that I use. How can I tell the browser to open the new page in _top not inside the IFRAME?
Thanks in advance.

You are not able to achieve the desired effect in PHP. This is something you'd have to do from JavaScript or add target attribute to <form>:
<form ... target="_top">

You can use javascript to access the parent. You could echo out javascript in your PHP.. so your parent page has this:
function changeURL( url ) {
document.location = url;
}
and in your php script, you echo
<script>
parent.changeURL('mypage2.html' );
</script>
The reason you can't call parent.document.location is because it's read only - you have to have a function available on the parent to do it.

A simple way directly in PHP to redirect the parent page rather than the iframe:
echo "<script>top.window.location = '/mynewpage.php'</script>";
die;
The die; isn't necessarily necessary, but it is good "just in case" to prevent the script from continuing any further, for example, if javascript is disabled in the user's browser.

we can use javascript like this :
target.window.location='locationpage.html';
top.window.location='mypage2.html';

The best and simplest thing to do is to use the form target element
<form action="..." target="_top">
<form action="..." target="_parent">
either of the target parameters works fine

You can either link to the page using Break Out or use code
<?php header("Location: pagename.php?Break=Y"); ?>
You then use the following code in the header of the page with
if(isset($_GET['Break'])) //
{
$BreakFrame = true;
$BreakToPage = "pagename.php";
}
<script language="JavaScript" type="text/javascript">
function changeURL( url ) {
document.location = url;
}
</script>
<?php if($BreakFrame) { ?>
<script language="JavaScript" type="text/javascript">
parent.changeURL('<?=$BreakToPage?>' );
</script>
<? }?>

In the page that you want to load in _top, add the follow code in the header
<script type="text/javascript">
if (top != self) top.location.href = location.href;
</script>

For CakePHP 4, to redirect your parent page just add option 'target'=>'_top' in your iframe's link:
Example:
<?= $this->Html->link(__('Redirect Parent'), ['controller' => 'Users', 'action' => 'view'], ['target'=>'_top']) ?>
All the best!

You can add multiple headers to your redirect.
I use a little function that I created with this in mind.
public function redirect($url){
header('Window-target: _top');
header('Location:' . $url, true, 303);
exit();
}

Related

What are some Alternative redirect methods to meta refresh?

I am redirecting to a new page after some specific time given by the user, like this :
html_meta_redirect( 'pagename?page_number='.$f_pageNumber, user_get_pref( 'refresh_delay' )*60 );
what are some alternate methods to implement this?
<?php
header('location:welcome.html');
?>
the header will redirect to the welcome.html page
Please note that there should not be any echo statements before the redirection
Try this:
<script>
function getLocation() {
window.location = 'placed your URL here';
}
window.setTimeout('getLocation', 5000) // 5000 = 5 seconds
</script>

redirect to a specific page from php

Hello I am using following code to open a page in PHP
header('Location :login.php');
but the problem is that this is doing inside the div of page, when I clicking on that the page is being loaded inside the DIV not on the fill screen, what should I write here to solve the problem?
edit
I am loading the page like this
javascript, in main.php
var div = document.getElementById('content');
var url = 'my.php';
div.innerHTML = '<iframe style="width:100%;height:100%;" frameborder="0" src="' + url + '" />';
and I give a link to logout in my.php, here I need to open a login page and destroy the main.php page
Instead of returning a redirect (which redirects only your frame) like you do now, you'll have to have the top frame redirected.
Since your code gets loaded in an IFRAME, you'll need to change the TOP frame position, instead or returning a header, return the following javascript:
<script type="text/javascript">
window.top.location = "http://www.example.com/path/login.php";
</script>
The trick is to use window.top instead of window.
You are displaying stuff before header is sent.
if ( !headers_sent() ) {
header("Location: login.php");
} else {
echo '<meta http-equiv="refresh" content="0;url=login.php">';
}
Javascript redirect.
<script type="text/javascript">
window.location = "http://www.domain.com/login.php";
</script>

Add Redirect URL link to JS Function

How can I add redirect URL link to JS function (example form action to : session.php) in below code.
I've tried with another way code, but it still can't function.
$(document).ready(function() {
$("#submit_butt").click(function() {
var conf = {
frequency: 5000,
spread: 5,
duration: 600
};
/* do your AJAX call and processing here...
....
....
*/
// this is the call we make when the AJAX callback function indicates a login failure
$("#login").vibrate(conf);
// let's also display a notification
if($("#errormsg").text() == "")
$("#loginform").append('<p id="errormsg">Invalid username or password!</p>');
// clear the fields to discourage brute forcing :)
$("#password").val("");
document.forms['login_form'].elements['username'].focus();
});
});
You can try this
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
https://developer.mozilla.org/en-US/docs/DOM/window.location
Ref: How to redirect to another webpage in JavaScript/jQuery?
you can use this..
window.location.href = "http://www.google.com";
you can try
<script>
function name(){
window.location ='abc.php';
}
</script>
you can by breaking into php code inside your javascript
$(document).ready(function()
{
<?php
someFunction();
?>
});
but only if your javascript is in a php file so it can be processed by php. So if your linking to a .js file that needs to be changed to .php

Trigger PHP function by clicking HTML link

Is it possible to trigger a PHP function by just clicking a link? Or should I stick with the form submit method? If clicking the link will work, where should the link refer to?
Here is a sample code:
<?php
session_start();
function listMe($username){
$username = mysql_real_escape_string($username);
$query = mysql_query("INSERT INTO List (Usernames) VALUES ('$username')") or die(mysql_error());
}
?>
<html>
<head>
<title>SAMPLE</title>
</head>
<body>
Add my username to the list
<?php
listMe($_SESSION['Username']);
?>
</body>
</html>
Maybe someone can point me in the right direction. Thanks!
You can do this by means of loading the entire page over again by the use of form submission, or by loading specific page contents directly into the page without needing to go from page to page. The second method is called "AJAX" (Asynchoronous Javascript and XML). Here are two examples, one of each specified.
Form submission approach
form.php
<?php
function get_users(){
}
if(isset($_GET['get_users']))
{
get_users();
}
?>
...
<form method="get">
<input type="hidden" name="get_users">
<input type="submit">
</form>
AJAX approach
ajax_file.php
<?php
function call_me(){
// your php code
}
call_me();
?>
form.html
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
// do something if the page loaded successfully
}
}
xmlhttp.open("GET","ajax_file.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
click to call function
</body>
</html>
HTML
list me
PHP
<?php
if (isset($_GET['list_me'])) listMe();
(EDIT although this works, it's a bad idea as it is. One should never read from $_GET without sanitising it first)
You can pass it as a query parameter of the link.
http://example.com/?command=listMe&username=tom
However that way everybody will be able to run the function by loading that URL
List me
and in the PHP
<?php
if( isset($_GET['list_me']) && isset($_SESSION['Username'] ) ){
listMe( $_SESSION['Username'] );
}
?>
To trigger a function on link click with php the only way I know would be to append a param in the url of the link and then listen for that
Add my username to the list
Then check for link
if (isset($_GET['function'])){
runFunction();
}
This is because php is a server side technology if you want to fire something without refreshing the page you would need to look at something like javascript
I found this code in a plugin, they have user a foreach look to trigger the action:
$actions = unset($meta[$key]);
foreach ( $actions as $action => $value ) {
echo '<li>' . '<i class="fa fa-times"></i></li>';
}

Change page dynamically

<?php
$go = $_GET['go'];
if(!empty($go)) {
if(is_file("page/$go.html")) include "page/$go.html";
else echo "<h1>Error 404</h1><p>Strona nie odnaleziona</p><p>Warunek ?go jest niepoprawny</p>";
}
else include "page/start.html";
?>
<script>
$.get('go', function(change) {
$('#center').load('page/<?php echo $go ?>.html');
});
</script>
I have somethng like this but it doesn't work. I just want that the page which is loaded (?go=name) won't refresh whole page
Firstly, that's vulnerable to an LFI vulnerability (Local File Inclusion). Consider what happens when someone enters: http://site.com/file.php?go=../../../../../../../../etc/passwd%00
Also, you can't just echo your filename and expect it to print it out...if you want to include the contents of the page in $go and print out $go, then use:
$go = urldecode([$_GET['go']);
$go = str_replace("/", "", $go);
$go = "page/$go.html";
EDIT: Actually, if you use my above code, they could still access files in the local directory, such as .htpasswd and .htaccess, so just don't let your users include any local files. There are better ways to solve the problem you have.
As for the AJAX, follow jeroen's advice.
You are mixing two ajax functions, $.get and .load and you are missing an event handler.
You will need something like this:
$('#go').live('change', function() {
$('#center').load('page/' + $(this).val() + '.html');
});
I have used live instead of change in case the go button is located in the refreshed section of the page.
Note that I am guessing that you want to refresh a section of your page based on a selection, the question / code isnĀ“t exactly clear about that...
Is this what you want to achieve?
<script type="text/javascript">
$(document).ready(function() {
var getgo = '<?php echo $_GET['go']; ?>';
if(getgo.length) {
$('#center').load('page/'+getgo+'.html');
}
});
</script>
<div id="center"></div>
It will load the contents of page/[your go param].html into #center.

Categories