PHP header pass parameters in url - 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>';
}
}

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.

run a if step by step PHP

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>";

PHP Header Function Not Working Well

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.

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

javascript redirecting to main window doesn't take into account http option (&)

i'm triying to use javascript redirect to main window after log in succeed in an iframe.
this is my code :
if ($_GET['redirect']!='') {
$redirect=$_GET['redirect'];
$smart->assign('redirect',$redirect);
}
$redirect=$_GET['redirect'];
echo $redirect;
if(isset ($_SESSION['user'])&&$_SESSION['user']!='') {
$user->email=$_SESSION['user'];
$user->addCorporate();
$user->signIn();
$user->loadSession();
echo("<script language=\"javascript\" type=\"text/javascript\">");
echo "document.write('redirecting...');";
if ($redirect!='') {
echo 'self.parent.location = "'.$redirect.'"';
} else
echo 'self.parent.location = "index.php"';
//echo $redirect;
// redirect($redirect);
echo "</script>";
}
the echo $redirect displays http://xxxxxxxx/play.php?action=play&id=d59541b89828da34e9a8345a1bdafe2b
but the redirection is made to http://xxxxxxxx/play.php? (without the php option)
This sounds pretty mysterious.
Here's how I'd proceed: Turn off JavaScript in your browser and examine your created JavaScript. Then, at least, you know whether maybe for some bizarre reason the wrong URL is print out after all, or whether the problem's in the redirection part.
If you change the line:
echo 'self.parent.location = "'.$redirect.'"';
to this:
echo 'alert("'.$redirect.'")';
What happens?

Categories