I'm new at HTML/PHP and a doubt just came to my mind while developing a simple system to the company I work for.
After the user fill an HTML form and save it, I have a php page that saves the previous form information in my MySQL database. This PHP page displays a message if tha data has been saved correctly and after 5 seconds it redirects the user to my index.php. I used php header function to countdown the 5 seconds and redirect the user to index.php:
header("refresh:5,url= index.php"); /* Redirect browser */
That works perfectly on Google Chrome, but on IE it seems like it just understand the "refresh" part and completely ignore the "url" redirect, because it just refresh the current page, which gives me a few errors, of course.
My doubt is: php header function has compatibility problems with IE11 (or vice versa)? I know that I can use HTML to do the same thing, but is there a way to make the "url" from php header function works with IE11?
Thanks.
you can use it to refresh page
<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">
please use like this as suggested below
echo '<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">'
That's not how you redirect in PHP (it's actually how you redirect in HTML, which isn't the same thing). Try;
header("Location: index.php");
Related
I’ve been battling with this for hours, I wonder if anyone can help.
I want to make a redirect script which first actions a link. I have a link generated by php which deletes the current user’s avatar. This link works (user avatar is deleted) however the link itself doesn’t lead anywhere, it just reloads whichever page it is launched from (I haven’t quite worked out how yet, I presume this is a feature of wordpress/buddypress which I am using). My aim is that on arrival to a particular page (page1.php), the delete avatar link is automatically actioned, and then the user is redirected to another page. So:
1) User arrives at page1.php
2) Script fires this link :
<a href="<?php if ( bp_get_user_has_avatar() ) : print 'mysite.net/members/'; echo userpro_profile_data('user_login', $user_id2); print '/'; bp_avatar_delete_link(); else : 'something-else.php'; endif; ?>"></a
3) User redirected to page2.php
I guess there may be some way to do this in javascript/ajax but I hardly use it so not really sure how. I’m struggling to get it to work in php also. Any help would be really appreciated.
Thanks.
You can redirect the page via Javascript using Location API:
<script type="text/javascript">
window.location = <?= $new_location ?>;
</script>
Or you can do it in PHP after performing required operations using code like this:
header("Location: {$new_location}");
But notice that if you redirecting via headers you should not echo enything to the page before it.
Or you can use wp_redirect() if youre doing it in Wordpress.
i create a login page.
when user submit "username" and "password" if the username and password was true the
page redirect with
header("location:home_page.php");
this code work in firefox but in ie8 doesn't work.
i search and understand that ie has problem with session(that sent with another page, like iframe and popup)
finally i found a solution that told me set a header for privacy policy (p3p).
and i set this header but my problem not solve.
please help me.
i use
echo '<script type="text/javascript">window.location.href="home_page.php";</script>';
but the problem not solved
Some time I can not discover why header errors appear then i use javascript header instead php like:
echo '<script type="text/javascript">window.location.href="redirectpage";</script>';
You can use html meta tag too with php for redirection.
so I made a simple user log in website, and I want to set a cookie so that when they come back to the website, it will take them to the members area and not the main page, kind of like a "remember me" function that redirects users to a members area if a cookie is set.
Problem I am facing: The php code right before the html code does not redirect to the member.php page even though the cookie is set!
Note: I'm just using parts of the code, and not the entire code/ other files to simplify the question.
here's my code:
main.php (this is the main page, and also where the log-in form is, but log in form is not shown)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php
//Checks if there is a login cookie
if(isset($_COOKIE["blablabla"])) //if cookie is set
{
header("Location: www.website.com/member.php"); //redirect to member.php
}
else
{
//otherwise, redirect to nocookiefound.php
header("Location: www.website.com/nocookiefound.php");
}
?>
<html>
<body>
<?php
echo "Welcome " . $_COOKIE["blablabla"] . "!<br />";
//I ran a echo test to see if cookie is still there, and it is.
?>
</body>
</html>
So my question is, can my redirect work the way it is?
I must be doing something wrong because it's not redirecting to member.php even though the cookie echos the correct value.
So, if i was originally in the members.php page after I logged in, then go back to main.php, it SHOULD redirect me to members.php, but it doesn't, it just stays at main.php. Anyone know what's going on? I would appreciate all the help I can get. Thanks
The PHP code should be the first thing on the page, as you're sending a redirect "Header". Move it to before the Doctype declaration.
As well as what #Dogbert says, your redirection is invalid. Try:
header("Location: http://www.website.com/member.php");
In addition to the other answers, put an exit(); after the header lines. It's possible to write a script that ignores headers, hence running the other code inadvertently.
People often use headers to protect admin areas without realising that.
I wanted to popup an alert box. After that, the site would redirect to main page. But, it seems that it directly redirect to the mainpage without alerting anything.
if($registerquery)
{
?>
<script>alert('received!')</script>
<?php
}
header("Location: mainpage.php");
exit();
?>
I wanted to do this to ensure users that the process of submission ended successfully. How can i alert something before the page redirect to mainpage and more importantly what causes this? I think the page should not have redirected before the alert box.(Before these codes, site registers what users submitted but not relevant i guess.)Thanks
You just can't do this. PHP is server-side, JS is client-side. Using a location header is server-side, so the browser never gets the JS.
Instead, try something more like:
if( $registerquery)
echo "<script>alert('received!'); location.href='mainpage.php';</script>";
and remove the header bit altogether.
I was wondering if there was any way through php or javascript I could tell the browser to go back to the page it came from, or even better not load the page at all (the later being probably impossible).
The reason for this is that I have written a small php script that will take parameters from the url and post a tweet for me discreetly while I am at work.
ex.
tweet.php?user=myname&pass=mypass&message=My message goes here
Though it works, I get stuck with a white page. It would be nice if I could have the browser go back to the page it was just on, so the pause between work would be minimal.
Thank you for the help!
javascript: history.go(-1);
The JavaScript function for this is window.back(). Have your PHP script produce something like the following to have browsers automatically "bounced back" to the submitting page:
<html>
<head>
<title>Success</title>
</head>
<body onload="window.back()">
<h1>Success</h1>
</body>
</html>
Non-JS browsers will see a "success" message, JS browsers will get bounced back.
You could do the following in PHP to redirect back to the previous page:
<?php
$ref = $_SERVER['HTTP_REFERER'];
header('refresh: 10; url='.$ref);
?>
Depending on the browser, either an HTTP response code of 204 or 205 might cause it to not leave the current page.
If use use a PHP HEADER, you can redirect to another point on the site. Minimal Pause work (as long as the process isn't very long).
In tweet.php, use the header function to redirect back to the referer
Sending people back to the page without any success message would be very confusing. I would make the call with AJAX and provide some feedback to the user that the action was performed successfully, very much like the voting system here on SO works.