I have a PHP code that has header and sleep Functions to display an alert as it redirects the page a bit slower. But the sleep does not seem to be working.
This is the PHP code, Please check:
<?php
if ($query) {
echo '<script language="javascript">';
echo 'alert("Registration SUCCESFUL")';
echo '</script>';
header('sleep(10);');
header('Location: http://localhost/amberTAG%20requester/Login%20Page/Login.html');
} else {
echo '<script language="javascript">';
echo 'alert("Registration UNSUCCESFUL, Sorry!")';
echo '</script>';
header('sleep(10);');
}
?>
Thanks,
Just use sleep without header
sleep(10);
header('Location: http://localhost/amberTAG%20requester/Login%20Page/Login.html');
Or use:
header('refresh:10; URL=http://localhost/amberTAG%20requester/Login%20Page/Login.html');
If you want to use sleep before alert, you should use setTimeout for JavaScript.
Try this:
setTimeout(function(){
alert("Registration UNSUCCESFUL, Sorry!");
}, 10000);
And, if you want to use sleep before URL redirection, try this:
header( "refresh:10;url=yoururl.html" );
Related
I'm trying to get a page refresh once after a set amount of seconds only on a set page.
So something like
if is_page('test.php') {
refresh page 5000(once);
} else
continue
You can do this by jQuery
like
window.setTimeout(function() {location.href="URL of test.php"}, 5000);
in PHP you can:
if(is_page('test.php') && !isset($_GET['r'])){
header("Refresh: 5;url='http://zasite.com/test.php?r=1'");
}
NOTE: This can only be done if the headers have not already been sent http://php.net/manual/ro/function.headers-sent.php otherwise you will end up with a warning and the refresh will not work. A workaround would be:
if(is_page('test.php') && !isset($_GET['r'])){
if(!headers_sent()){
header("Refresh: 5;url='http://zasite.com/test.php?r=1'");
} else {
echo '<meta http-equiv="refresh" content="5" ; url=http://zasite.com/test.php?r=1>';
}
}
OR - PHP with Gyandeep Sharma's JS answer
if(is_page('test.php') && !isset($_GET['r'])){
if(!headers_sent()){
header("Refresh: 5;url='http://zasite.com/test.php?r=1'");
} else {
echo '<script>window.setTimeout(function () {location.href="http://zasite.com/test.php?r=1";}, 5000);</script>';
}
}
i have a problem in PHP, i want first show a message like this:
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
and when i clicked on ok, then redirect to another page, my code is like this:
$fgmembersite->RedirectToURL("home2.php");
complete code:
if(isset($_POST['submitted']))
{
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
$fgmembersite->RedirectToURL("home2.php");
}
but this code do not show me the message just redirect to home2.php, if there is another way except of 'if' please explain. the simplest way
thanks
If you just want to redirect to another page.. you can do this:
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
echo '<script>window.location.href = "the-target-page.php";</script>';
you should use javascript o redirect only. You are trying to mix, both php and javascript
echo "<script>javascript:alert('message successfully sent'); window.location = 'home2.php'</script>";
I have this code which relocates a user to index.php if they set the value of a dropdownmenu
on an irrelevant page to set it for please check my code.
if(isset($_GET['d'])&&empty($_GET['d'])===false){
$cur_page=$_SERVER['PHP_SELF'];
$current_page = substr($cur_page,1);
$possible_page = array('terms.php','contact.php','about.php');
if(in_array($current_page,$possible_page)){
header('Location:/index.php?d='.$_GET['d'].'');
exit();
}else{
echo $_GET['d'];
}
It works fine on my localserver but on live server it does not ?
add ob_start(); at very beginning of the php script. If it include another file then do not use ?> in the end. Thanks
I always use this little method and works perfect in all situation.
public static function Redirect($sec, $file)
{
if (!headers_sent())
{
header( "refresh: $sec;url=$file" );
}
elseif (headers_sent())
{
echo '<noscript>';
echo '<meta http-equiv="refresh" content="'.$sec.';url='.$file.'" />';
echo '</noscript>';
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$file.'";';
echo '</script>';
}
}
In last case it will redirect for sure.
I have an index.php for registration, a login.php as the registration handler. when the registration is failed, it sent out the error message to client. I use this code for sending out the message
header('Location: http://localhost/musicshare/index.php?reg=false');
and receive in index.php
if(isset($_POST['reg'])){
echo 'set';//for checking whether reg is set
if($_POST['reg']=='false'){
echo '<script type="text/javascript">';
echo 'alert("Reg faile")';
echo '</script>';
}
}
However, It seems not working at all; Please help, thanks.
Use $_GET instead of $_POST:
if (isset($_GET['reg']) && $_GET['reg'] == 'false'){
// do something
}
When you use a header() it fires a GET request.
So you should use $_GET['reg']
if(isset($_GET['reg'])){
echo 'set';//for checking whether reg is set
if($_GET['reg']=='false'){
echo '<script type="text/javascript">';
echo 'alert("Reg faile")';
echo '</script>';
}
}
I have a function which echoes javascript to navigate to a different page. While navigation occurs, the
echo 'window.location.href="'.$url.'";';
does not work and simply prints it on the screen.
"window.location.href="./index.php";
I use my function this way: redirect("./index.php");
My php function is as follows
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
Your browser treats the response as plaintext.
Prepend to you response a Content-Type: text/html\n plus wrap your content inside an <html></html> tag.
Try this way.
<?php
$yourURL="http://www.stackoverflow.com";
echo ("<script>location.href='$yourURL'</script>");
?>
Why not just use output buffering and not have to deal with JavaScript or meta redirects at all?
<?php
// Top of your page
ob_start();
// Code goes here
// Redirect
if ($redirect_is_necessary)
{
header('Location: '.$url);
exit;
}
// Rest of page goes here
// Bottom of page
ob_end_flush();
?>