PHP Header Function Not Working Well - php

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.

Related

Php Header redirects, but doesnt echo after that

It redirects them back to the homepage but I want it to also display a box.
if($m->send()){
header('Location:http://blankwebsite.com/');
echo '<script>
alert("Your Quote Request has been submitted!");
</script>';
}
else{
echo $m->ErrorInfo;
}`
You could save the html in a session variable, display it after redirect and empty the session variable like a flash message at script start up.
if($m->send()){
$_SESSION['redirectMessage'] = base64_encode(utf8_encode('<script>alert("Your Quote Request has been submitted!");</script>'));
header('Location:http://blankwebsite.com/');
}
else{
echo $m->ErrorInfo;
}
On new page request or redirect:
if isset($_SESSION['redirectMessage']) {
echo htmlentities(base64_decode($_SESSION['redirectMessage']), ENT_QUOTES, 'utf-8');
$_SESSION['redirectMessage'] = null;
}
This solution uses sessions, so do make sure session_start() is called at the top of the script.

Redirect fails in PHP

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

Header Location dont work on live server but works on localhost

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.

echo Javascript window.location.href not working

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();
?>

Redirect to a local webpage created dynamically in www Wamp folder

I use the PHP code above to create a local page using Wamp in the www folder and I'd like to redirect to this page but the redirection with header doesn't work.
I get the message :
The page isn't redirecting properly.
Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies
Does someone know how to do this ?
my code :
$myFileName_with_no_lower_case = 'website_name'.$job.$ville;
$myFileName = strtolower($myFileName_with_no_lower_case);
$myFileHandle = fopen($myFileName, 'w') or die("can't open file");
$file_content = file_get_contents('./file_with_content_i_want_to_paste.php');
fwrite($myFileHandle,$file_content);
fclose($myFileHandle);
// eveything works fine until now
header("Location:".$myFileName);
this is your solution, one of my faverite functions in my toolbox :)
//==== Redirect... Try PHP header redirect, then Java redirect, then try http redirect.:
function redirect($url)
{
if (!headers_sent())
{ //If headers not sent yet... then do php redirect
header('Location: ' . $url);
exit;
} else
{ //If headers are sent... do java redirect... if java disabled, do html redirect.
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;
}
} //==== End -- Redirect
Works now :
$myFileName_with_no_lower_case = $job.$ville;
$myFileName = strtolower($myFileName_with_no_lower_case);
$myFileHandle = fopen($myFileName, 'w') or die("can't open file");
$file_content = file_get_contents('./formulaire_artisans.php');
fwrite($myFileHandle,$file_content);
fclose($myFileHandle);
header("Location:".$myFileName);

Categories