When using this code to try an show an error, the page redirects to myaccount, but should only redirect there if the required field has been completed, for example;
error message appears if birth_country is not chosen
if(empty($birth_country))
{
$err[] = "ERROR - Enter Birth Country";
header("Location: personal.php?msg=$err[0]");
}
but if it is chosen, should redirect to...
header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
exit();
However it always goes to myaccount no matter what
Thanks
After your first header put exit as well. Your code just continues on and it likely emits the second location header as well, negating the first.
Like Frits says, you will have to print out the error to the screen if you want it to be seen; however, you can not print out any HTML before a header or the header won't work. If you want to print the error on the redirecting page then I would suggest putting the error message in a $_SESSION cookie, then call and echo that cookie/variable on the redirecting page. But by doing this, you will need to use php session_start() at the top of the question page and the top of your redirect page in order to use the $_SESSION variables
Also, your second header
header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
exit();
Should look like this:
header("Location: myaccount.php?id=$_SESSION['user_id']")
By using the double quotes, you don't need to add the $_SESSION[] tag, then add blank "" at the end.
Related
I can't find this anywhere right now and looking for a quick answer.
What is the functionality of using webpagename.php?error=1?
Basically I want to use it so that when an error occurs logging in, I can redirect them to the page with a different message saying error.
I tried using
header('Location: loginpage.php');
echo '<p class="error">Error Logging In!</p>';
but nothing ever shows up. Thanks!
in two way you can do this!
-1:
use like that parameter in URL
header('Location: loginpage.php?error=1');
in loginpage.php in JavaScript section check URL is contain error or not!
look at this link
just replace "q" with error!
make a function and check if contain error
show a error message!
<script>
function logincheck(){
// code
}
if(logincheck()){
// do something
//alert('error');
// or append a error in html element
}
</script>
you can check this query string in php but JavaScript way is better
-2
use session
after validate set a session with error message
like this:
$_SESSION['login_error'] = 'error in login!';
and in loginpage.php
check if that session have a value, show that to user,and unset session
like this:
if(isset($_SESSION['login_error'])){
echo '<p class="error">Error Logging In!</p>';
unset($_SESSION['login_error']);
}
when u doing header, you already redirecting to the loginpage.php
you should put the error message at loginpage.php.
or you can hold the redirection by doing something like
header('Refresh: 3;url=loginpage.php');
which redirect after 3 second;
On the page processing the login check, if login fails:
header('Location: loginpage.php?error=1');
Inside of loginpage.php place code where you want the message to appear:
<?php
If ($_GET['error']==1) {echo "login error!"} elseif ($_GET['error']==2) { echo some other message } .....
?>
The purpose of ?error=1 in the url is to pass a variable named ERROR with a value of 1 through the URL which is accessable to the redirection target page through $_GET
I am currently creating a podcast website where I have a form for my users to enter a comment on the current episode they are listening to.
All my content is stored in mysql and data on my episode page is displayed like so
http://www.mysite.com/episode.php?id=1
With separate content for the episode.
On this post adding to database no repeat on refresh
I mentioned about the form resubmitting on refresh which is why I added
header('Location: index.php');
However. I would like this to visit the same page with a thank you message echoed.
I have tried
header('Location: episode.php?id=<?php echo $data['cast_id']; ?>');
echo "thank you";
But this brings back the error Parse error: syntax error, unexpected T_STRING
Please can someone advise me on the best way to do this? Thank you.
header('Location: episode.php?id=<?php echo $data['cast_id']; ?>');
you are using php tags in already started php tags.
you can simply do this ..
session_start(); // make sure session is up and running
// just before redirecting
$_SESSION['thankyou_msg'] = 1;
header('Location: episode.php?id='.$data['cast_id']);
now on index.php.
session_start();
if(isset($_SESSION['thankyou_msg'])){
echo "your thankyou message here";
unset($_SESSION['thankyou_msg']);
}
header() function is php function, so why you start php in it?
Also add exit() and store message in SESSION variable.
$_SESSION['msg'] = "thank you";
header("Location: episode.php?id=$data['cast_id']");
exit();
I made a simple login-system in php and mysql, but I keep getting errors saying that headers already been sent, and using ob_start fixes this problem, but im not sure if I should then use ob_clean at the footer afterward?
Also, the error comes when I have logged in to the account page, saying header already been sent in previuos page - > header("Location: account.php"); But I have to redirect the user when they login.
My login page looks like this
require_once('models/init.php'); // db connection and other functions
include('header.php'); // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"
{
php code to check if username/pass matches etc, and if so redirect to account page
header("Location: account.php");
}
echo "<form>" // display the login form
include("footer"); // including footer, some html/js code.
This code above works if I use ob_start in the header.php file. But should I use ob_clean afterwards in the footer.php file?
Sorry if anything is unclear, english is not my first languish
Thanks!
The general principle is you cannot use echo before header(). So, this will never work:
echo "this is my header";
header("Location: account.php");
echo "this is my footer";
However, if you sent the headers first, everything works fine:
header("Location: account.php");
echo "this is my header";
echo "this is my footer";
In your case, you should do the check before you include the header:
require_once('models/init.php'); // db connection and other functions
if ($user_is_logged_in) { // Do your check here
header("Location: account.php");
}
include('header.php'); // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"
echo "<form>" // display the login form
include("footer"); // including footer, some html/js code.
ob_start() captures the output (what would otherwise be printed or echoed). If you don't need to echo the output or to do anything with it then just use ob_end_flush() when you are finished.
I have a page, q.php, that is a user submitted post defined by its id, (for example, q.php?id=1 would be a certain post that uses the $id variable to pull all the rest of the information from the database).
I am trying to code a comment submission on the same page, and account for the fact that a user might not enter anything into the field. If this happens, I want the page (e.g. q.php?id=1) to load again with an error message.
I can do the error message with an empty variable that is then given a value by the php file that the form activates. However, I am having a problem navigating back to the specific post. I tried to use include('q.php?id=$id) where $id is set to a number, but I understand that this is not its purpose and it does not accept variables
What should I be using instead?
EDIT
answer.php (file that the form activates):
require 'q.php';
$_GET['id'] = $id;
$_SESSION['error'] = "Please fill in all of the fields.";
q.php:
if ($_SESSION['error'] !== 0) {
echo "<p style='color: #AA1111;'>".$_SESSION['error']."</a>";
unset($_SESSION['error']); // this isn't happening...
}
If you really must include the page inline, you can always modify $_GET:
$_GET['id'] = $id;
require 'q.php';
But an error message sounds like it could be accomplished with a session variable or a redirect. A redirect could look something like:
header('Location: q.php?id=' . $id . '&error=The+error');
exit();
Then, you check for $_GET['error'] in q.php. Using a session variable for that would be much the same, except instead of adding error as a querystring parameter, you use $_SESSION['error'] and unset it immediately.
You could use header("Location: q.php?id=$id"); exit;, but you would need some other way to send the error message (example: save it in $_SESSION)
You might be able to set the $_GET array how you want it - in this case, $_GET = Array("id"=>$id); then include("q.php"). This is generally considered haxy, though, and may result in problems if you don't use include_once properly.
I know in the Manuel it says that the header has to be the first thing in a script, but how come I see some codes where header("Location: member.php?id=$username") is in a if-statement?
Ex:
//a bunch of codes above
if($result!="0"){
// authenication correct lets login
$_SESSION["password"] = $password;;
$_SESSION["username"] = $username;
header("Location: member.php?id=$username");
}
else
{
echo "Wrong username or password. Please try again!";
}
But when I do this, it sometimes would/won't throw an error.
How do I allow the header (); to be used in a script without any errors?
I want to redirect the user back to the login if they click "no" and to the homepage if they click "yes".
It doesn't have to be the first thing in the script. But it haves to be the first thing that you output to the user. You MUST NOT echo stuff before using the header function. If you don't, you can use it at any place you want.
You could also "ignore output" using ob_start and ob_end_clean.
Best regards,
T.
You have to use output buffering so that nothing is sent back to the browser until the entire operation is complete. Check out the ob_start function, that should give you a good starting place.
ob_start
Without output buffering, the script sends header info back to the browser, and once that has happened you cannot use header() to redirect.
header('Location: member.php?id='.$username);
You might want to change and use this code instead if you are just going to send or $_GET['username'] an value from your previous session.