PHP header redriction to new page - php

I would like to click on a hyperlink which will go to page 2, but I also want to open a new blank page.
I tried:
header "Location: http://bing.com";
header ("Location: http://bing.com", false);
header ("Location: http://bing.com", true, 301);
I'd like to use only php to open the new blank page.

You cannot do this from the header redirection. You need target="_blank" attribute added to your <A> in HTML. Or do the trick where your redirection redirects to the page that would use JavaScript to do that for you (expect it to be blocked by the browsers anti-popup feature though)

You cannot open a new window from doing a header redirection. You must use Javascript.
If you need to do it with PHP for some reason, you could print the Javascript like this:
<?php
echo '<script>window.open("http://www.google.com/");</script>';
?>

This is not possible with PHP. If you want to open 2 pages at once (replace the current, and open a second tab/window) you must do this with javascript.

One of the perfect solution to it which I found here only and you can do one thing is that which page you want to open along with other one put the code below in a PHP page the one needs to be opened and this code will pop up for the link provided in place of default link.
<?php
echo "<script type=\"text/javascript\"> window.open('http://reliable4you.com/', '_blank') </script>";
?>

This is my code for new tab and its working perfectly.
Just only care about your file location.
CODE:
<?php
if(isset($_POST['button']))
{
echo '<script language="javascript">';
echo 'window.open( "/Projectfolder/File.php" )';
echo '</script>';
}
?>

Related

How to add text in a php page

I have a PHP code to redirect on other domain, this one
<?php
header("Location: {$_GET['url']}");
echo 'You are redirecting to ... page';
sleep(5);
?>
And it works like when i add any external url at the end of http://my-domain.com/URL.php?url= it just redirects to external domain in this case let's take google:
http//my-domain.com/URL.php?url=http//www.google.com
and it just redirects directly on the page after 5 seconds load but i want to show a php page with some decoration like a loading icon and some text like "Please wait while you're being redirected to http://www.google.com"
Also i tried echo 'Please wait while you're being redirected'; But it just redirects directly to external domain without showing the message.
If you don't want javascript to do the timeout, you can add the refresh timer to the php header function:
header("Refresh: 5; url={$_GET['url']}");
Or go super old school and spit this out in the html:
echo '<meta http-equiv="refresh" content="5; url='. $_GET['url'] .'">';
Edit: I'd like to mention if you go old school, be sure to sanitize the url (dont just pass the GET variable out blindly like the above sample).
Try This
<?php
echo "Please wait...";
echo sprintf('
<script>
setTimeout(function(){
window.location.href = "http://stackoverflow.com";
},2000);
</script>
');

Not using the url from a header redirection

I am using a header redirection to go back to a pagejump spot at my webpage. I did this with header('Location: index.php#contact');.
The problem I have now is that it says
www.pagename/index.php#contact
The problem is that I don't want to see the #contact in my url, how can I fix this?
Btw, the page were I use header('Location: index.php#contact'); is a different page as the one were I have the anchor.
I suggest that you add some javascript code at [index.php] to change the URL. Like this:
echo "<script type='text/javascript' language='javascript'>
history.pushState({},\"index.php#contact\",\"index.php\");
</script>";
The second parameter is the original URL, and the third parameter is the URL you want to display.
That's all.O(∩_∩)O~

unable to open two links at a time using window.open() and header functions

on opening of page i want 2 links to open one from field "link" and other "http://myprojects.com"
the code works without window.open link i.e without trying to open myprojects.com in a new window but gives error call to undefined function open(); when window.open is present.
without window.opem it redirects to the link from the link custom field
<?php
ob_start();
get_header();
$value= strip_tags(get_field('link',$post));
window.open("http://myprojects.com","_blank");
if( $value ) {
header('Location: '.$value);
die();
} else {}
get_sidebar();
get_footer();
?>
<?php
ob_end_flush();
?>
i understand that window.open() is a javascript code...how else do i get another link to open as a popup or second window
it's not php - it's javascript
window.open("http://myprojects.com","_blank");
window.open() will not open in new tab if it is not happening on actual click event. In the example given the url is being opened on actual click event. This will work provided user has appropriate settings in the browser.
You have added window.open("http://myprojects.com","_blank"); inside the php tag.
Its a javascript code not a php one..
`

Refresh a page using PHP

How can I refresh a page using PHP periodically? If I can not do it by PHP, what is the best recommended scenario?
You can do it with PHP:
header("Refresh:0");
It refreshes your current page, and if you need to redirect it to another page, use following:
header("Refresh:0; url=page2.php");
In PHP you can use:
$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");
Or just use JavaScript's window.location.reload().
You sure can refresh a page periodically using PHP:
<?php
header("refresh: 3;");
?>
This will refresh the page every three seconds.
That is simply possible with header() in PHP:
header('Refresh: 1; url=index.php');
Besides all the PHP ways to refresh a page, the page will also be refreshed with the following HTML meta tag:
<meta http-equiv="refresh" content="5">
See Meta refresh - "automatically refresh the current web page or frame after a given time interval"
You can set the time within the content value.
I've found two ways to refresh PHP content:
1. Using the HTML meta tag:
echo("<meta http-equiv='refresh' content='1'>"); //Refresh by HTTP 'meta'
2. Using PHP refresh rate:
$delay = 0; // Where 0 is an example of a time delay. You can use 5 for 5 seconds, for example!
header("Refresh: $delay;");
header('Location: .'); seems to refresh the page in Chrome, Firefox, Edge, and Internet Explorer 11.
Echo the meta tag like this:
URL is the one where the page should be redirected to after the refresh.
echo "<meta http-equiv=\"refresh\" content=\"0;URL=upload.php\">";
You can refresh using JavaScript. Rather than the complete page refresh, you can give the contents to be refreshed in a div. Then by using JavaScript you can refresh that particular div only, and it works faster than the complete page refresh.
Adding this meta tag in PHP might help:
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=' . $location . '">';
One trick is to add a random number to the end of the URL. That way you don't have to rename the file every time. E.g.:
echo "<img src='temp.jpg?r=3892384947438'>"
The browser will not cache it as long as the random number is different, but the web server will ignore it.
Add the following function to your project:
function redirect($filename) {
if (!headers_sent())
header('Location: '.$filename);
else {
echo '<script type="text/javascript">';
echo 'window.location.href = \''.$filename.'\';';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url=\''.$filename.'\'" />';
echo '</noscript>';
}
exit();
}
function call:
redirect($_SERVER['REQUEST_URI']);
PHP is server-side language, so you can not refresh the page with PHP, but JavaScript is the best option to refresh the page:
location.reload();
The visit Location reload() method.
You cannot do it in PHP. Once the page is loaded, PHP dies and is out of control.
You have a few options:
Use JavaScript
Use the refresh meta tag, <meta http-equiv="refresh" content="5">
I think that the refresh meta tag is the easiest and most convenient.

PHP refresh window? equivalent to F5 page reload?

Is there anything in PHP that is the equivalent of manually pressing the F5 page reload button? My php script is in a frame and isn't the parent script but it needs to refresh the entire page and not just it's frame.
Actually it is possible:
Header('Location: '.$_SERVER['PHP_SELF']);
Exit(); //optional
And it will reload the same page.
With PHP you just can handle server-side stuff. What you can do is print this in your iframe:
parent.window.location.reload();
If you have any text before a
header('Location: http://www.example.com/youformhere.php');
you'll have issues, because that must be sent before any other text is sent to the page.
Try using this code instead
<?php
$page = $_SERVER['PHP_SELF'];
echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
?>
Just remember, this code will create and infinite loop, so you'll probably need to make some conditional changes to it.
PHP cannot force the client to do anything. It cannot refresh the page, let alone refresh the parent of a frame.
EDIT: You can of course, make PHP write JavaScript, but this is not PHP doing, it's actually JavaScript, and it will fail if JavaScript is disabled.
<?php
echo '<script>parent.window.location.reload(true);</script>';
?>
<?php
echo "<script>window.opener.location.reload();</script>";
echo "<script>window.close();</script>";
?>
with php you can use two redirections.
It works same as refresh in some issues.
you can use a page redirect.php and post your last url to it by GET method (for example).
then in redirect.php you can change header to location you`ve sent to it by GET method.
like this:
your page:
<?php
header("location:redirec.php?ref=".$your_url);
?>
redirect.php:
<?php
$ref_url=$_GET["ref"];
header("location:redirec.php?ref=".$ref_url);
?>
that worked for me good.
Use JavaScript for this. You can do:
echo '
<script type="text/javascript">
parent.window.location.reload(true);
</script>
';
In PHP and it will refresh the parent's frame page.
guess you could echo the meta tag to do the refresh in regular intervals ... like
<meta http-equiv="refresh" content="600" url="your-url-here">
All you need to do to manually refresh a page is to provide a link pointing to the same page
Like this:
Refresh the selection
Adding following in the page header works for me:
<?php
if($i_wanna_reload_the_full_page_on_top == "yes")
{
$reloadneeded = "1";
}
else
{
$reloadneeded = "0";
}
if($reloadneeded > 0)
{
?>
<script type="text/javascript">
top.window.location='indexframes.php';
</script>
<?php
}else{}
?>

Categories