Here is a piece of code in PHP written in a separate file called core.php and this is included in register.php
if(($retuserkey = $this->dbcontroller->dbregister($email, $contact)) > 0){
//if user was successfuly registered send an email where he can activate his account!
$this->mailer->sendForReg($email,$hash,$flname);
echo "<script>alert('An activation link is sent to your email id. Please check you spam folder too. Please follow the link to complete the registration.'); window.location = './registersuccess.php';</script>";
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.websiteaddress.com/registersuccess.php">';
$url = "http://www.websiteaddress.com/registersuccess.php";
if(!headers_sent()) {
//If headers not sent yet... then do php redirect
header('Location: '.$url);
exit;
} else {
//If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
}
mail("emailaddress#gmail.com","Registration $retuserkey",$email,"Subject");
}
Emails are sent both before and after the redirect. Both the emails are received but the redirect fails.
What am I doing wrong?
header() must be called before any actual output is sent and you have echo before it.
You can't do:
header('Location: '.$url);
exit;
after something has already been echo'd out: http://php.net/manual/en/function.header.php.
If you move your echo statements so they only print when the redirect isn't happening, then you should be OK.
header('Location: http://localhost/yourFileName.php');
exit;
header() function is used /* Redirect to a different page in the current directory that was requested */
header() function sends a raw HTTP header to a client
N.B: use right url
Related
I have one file "djakhiltalreja_video.php" and another file "mobile_djakhiltalreja_video.php".i just want to redirect to this link http://akhil.djmusicweb.com/mobile_djakhiltalreja_video.php , current page :- djakhiltalreja_video.php .
but redirected url is http://akhil.djmusicweb.com/mobile_mobile_djakhiltalreja_video.php .
why double occurence of mobile_ ???
<?php
$pagename = "mobile_".basename($_SERVER['PHP_SELF']);
header('Location: http://akhil.djmusicweb.com/'.$pagename);
exit();
?>
Note : Remove mobile prefix from your page-name. I think it's covered in to $_SERVER['PHP_SELF']
Please check below solution for your problem.
Solution :
$pagename = basename($_SERVER['PHP_SELF']);
$url = "http://akhil.djmusicweb.com/".$pagename;
if (!headers_sent()) {
header('Location: '.$url);
exit;
} else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
exit;
}
This simple code will do the trick for you. It will check if headers are not sent, then it will call the PHP’s header function to redirect. But if the headers are sent, it will use Javascript to redirect to the URL you want.
I am try to develop flash message using sessions in php
suppose on successfully delete query I am setting
$_SESSION["msg"]="record deleted successfully";
header("location:index.php");
and I have the following script on all pages which checks if msg variable is available it echo its value as below
if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"]))
{
$msg=$_SESSION["msg"];
echo "<div class='msgbox'>".$msg."</div>";
unset($_SESSION['msg']); //I have issue with this line.
}
if I comment
unset($_SESSION['msg']);
message is being displayed, but with this line message is not being displayed
what am I doing wrong, or any alternative.
You are saying that you have that script on every page. So my guess is that after you make header("location:index.php"); your code continues to run - your message is displayed and unset (you don't see it because of redirect to index.php). When you are redirected to index.php your message is already unset.
Try adding exit; after header("location:index.php");.
Edit: I will add two examples with one working and one not. To test you need access test page with following link - /index.php?delete=1
In this example you will never see message. Why? Because header function does not stop code execution. After you set your session variable and set your redirect your code continues to execute. That means your message is printed and variable unset too. When code finishes only than redirect is made. Page loads and nothing is printed because session variable was unset before redirect.
<?php
session_start();
// ... some code
if ($_GET['delete']==1) {
$_SESSION["msg"] = "record deleted successfully";
header("location: index.php");
}
// ... some code
if (isset($_SESSION["msg"]) && !empty($_SESSION["msg"])) {
$msg = $_SESSION["msg"];
echo "<div class='msgbox'>" . $msg . "</div>";
unset($_SESSION['msg']);
}
// ... some code
?>
But this code probably will work as you want. Note that I have added exit after header line.
You set your message, tell that you want redirect and tell to stop script execution. After redirect your message is printed and unset as you want.
<?php
session_start();
// ... some code
if ($_GET['delete']==1) {
$_SESSION["msg"] = "record deleted successfully";
header("location: index.php");
exit;
}
// ... some code
if (isset($_SESSION["msg"]) && !empty($_SESSION["msg"])) {
$msg = $_SESSION["msg"];
echo "<div class='msgbox'>" . $msg . "</div>";
unset($_SESSION['msg']);
}
// ... some code
?>
You clearly said that you have that code (message printing) on all pages. If your code is similar to my example than adding exit should fix your problem.
Another problem might be that you are doing more than one redirect.
You can simply set your session empty or null instead of unset it. Just do:
$_SESSION['msg']=NULL;
Or
$_SESSION['msg']="";
I can't get to send visitors to the Thank you page using ECHO method.
Please help!
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
if (($name=="")||($email==""))
{
echo "All fields are required, please fill THE FORM again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Subscription Request from Website";
mail("test#test.com", $subject, $message, $from);
echo "Location: thankyou-subscription.php";
}
}
?>
Please use the header function to redirect users.
Example:
header("Location: thankyou-subscription.php");
You should also exit after echo "All fields are ...".
echo does not redirect unless it outputs some JavaScript code or HTML Meta Refresh. If you have to redirect via only PHP, use header and also make sure nothing is sent to browser before that header, not even a blank line
echo "Location: thankyou-subscription.php";
should be
header("Location: thankyou-subscription.php");
Edit
Since you mentioned you cannot avoid echo before redirect, you can use JavaScript because PHP redirect won't work after output. You can do
echo "<script>location.href='thankyou-subscription.php';</script>";
Don't echo, instead do this:
header("Location: thankyou-subscription.php)";
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>';
}
}
Probably something simple I'm missing, but what I'm trying to do is finish up a registration form and after it sends a confirmation email, redirect it to another page. What I have so far
if($sentmail){
<redirection code>
}
else {
echo "Problem sending information, please resubmit.";
}
You want header(), and possibly an exit to stop processing.
if ($sentmail) {
header('Location: http://example.com/after_true_statement_redirected');
// exit; ???
} else {
echo "Problem sending information, please resubmit.";
}
You do this by using the header(); function.
if($sentmail){
header("Location: http://mypage.com/redirect.php");
die();
}
else {
echo "Problem sending information, please resubmit.";
}
Use die(); to stop script execution. Though the page redirects, the script still executes.
The most easy redirect call is http_redirect in PHP. Does everything you need to do for a redirect.
if ($sentmail)
{
http_redirect('new location');
}
else
{
echo "Problem sending information, please resubmit.";
}