Sending Error Messages to Another Page? - php

When a user creates their own account, if there is an error I want to redirect them to the same page but display the errors at the top.
What's the best way to do this?

Structure your page as follows (in rough pseudo-code)
if (doing a post) {
process input
if (post is ok) {
redirect to success page
} else {
build error messages
}
}
if (error messages available) {
display errors
}
display form(filled in with previously submitted values)

i like to create a function named set_feedback() that sets the error in a session variable. then i have this other function get_feedback() that retrieves the information and unset the variable.

I save the error into the session and remove it once it was rendered. As you will probably abort the pages execution on a redirect it will never reach the code responsible for rendering thus leaving it in the session.

Related

how to remove a parameter from url on page refresh php

I am sending error values in the url.For example if i have a website named
www.example.com
and the url for login page is
www.example.com/login.php.
If the user enters wrong credentials url will be
www.example.com/login.php?invalid.
So everytime i refresh url remains
www.example.com/login.php?invalid.
How to remove invalid from url on refresh???
I think that by using the invalid GET variable you try to determine whether or not to display the error message to the user. This isn't really a good way to do so, due to the number of reasons, one of which made you ask this question.
You have a number of options instead, one of which would be using the session variables to store the error message. E.g., if the user login fails, you could store the message in your session:
if (badLogin()) {
$_SESSION['errorMessage'] = "Something's wrong";
}
and then on the login.php page you could try and see if it exists:
// ...your HTML...
if (!empty($_SESSION['errorMessage'])) {
echo $_SESSION['errorMessage']; // show it to the user
unset($_SESSION['errorMessage']); // so as not to display it every time
}
// ...your HTML continues...
This is not the perfect way either, but without knowing your application structure it's hard to suggest anything else.

how to display the content before exit() in php?

I'm using codeigniter. And in one of my admin controller, after a admin was logged in, I wanted to create a switch to check his admin level. If his level is below a certain number, he cannot view a certain page. And instead, a restriction notice page will be loaded and then the page will stop loading.
function add_user() {
$this->set_admin_level(9);
// stuffs
}
private function set_admin_level($level){
if ($this->session->userdata('admin_level') < $level ) {
$this->load->view("admin/restriction");
exit();
}
}
I tried exit() and die(), but both of them just killed the entire page without displaying anything. I know that I can do this with an if and then redirect method, but I wanted to know if I can do it the previous way.
try this:
private function set_admin_level($level){
if ($this->session->userdata('admin_level') < $level ) {
return $this->load->view("admin/restriction");
}
}
die or exit will stop all PHP processing.
If you don't want an error message on a blank page, you will need a page/view to display the error message. Then you either redirect the user to that page, or run the page in PHP before exiting, for example using $this->load->view.
Either way, you need to create the page or the view first, since you cannot display something by doing nothing.
And before displaying the actual error page, please set the HTTP 403 header to indicate to browsers and search engines that this is an Access Denied error page, not a normal page.

Firefox msg, need to be avoid

To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.
I am getting this firefox error. Though I am unsetting all variables at end of page using <?php unset[$_POST] ?>.. But if I update some record or update page again using this. Than I got above error.
After processing the request you should made a redirection to the same page to avoid such type of warning.
Saying OK to the warning message above will resubmit your form again and the PHP processing will be repeated. This should be avoided otherwise your database will have duplicated records if there is an INSERT query is getting processed.
header('location:http://www.example.com/currentpage');
die();
EDIT
You should do it something like below:-
if(isset($_POST['submit']))
{
//filter the data and validate user input
//do some stuff
/* Redirect users back to same url instead of refreshing page with javascript*/
header('location:http://www.example.com/currentpage');
die();
}

Posting array and redirecting to another page

I have signup form that posts all variables to signup.php. What I want to do is, during submit process collect all error codes to $err[] array and if submit process failed redirect user to msg.php and post $err array.
Then msg.php gets error messages from database with sent error codes from signup.php.
How can I pass array and redirect page to msg.php ? Is it possible with Location: header or something else?
Your architecture is wrong. You should not redirect a user to another page just to show the error messages. Why cant you just show the error messages on the same page.
Consider altering your application flow. But if you insist on doing something like this then you can use sessions for this. In signup.php if validation fails
if(!validation)
{
$_SESSION["err"] = $error;
}
Then in msg.php you can access the session variable easily as
foreach($_SESSION["err"] as $err) {
echo $err;
}
But if this is what you intend to do there is much better ways to do this and consider altering your flow to do in a better way.

Display message only for the first page load

I would like to display thank you message after adding comment only for the first page load... The comment form is processed using an external php file and than redirected back to the page. I would like to display some message after the redirection only... What would be the best way to do this using php?
Assuming you have access to the external php file that processes the file you could do something similar to the following on the processing file:
$_SESSION['flashMessage'] = 'Thank you for posting.';
header("Location: your-page.php');
And then add the following to the redirect page:
if ($_SESSION['flashMessage']) {
echo $_SESSION['flashMessage'];
$_SESSION['flashMessage'] = NULL;
}
Save the mesage into a session. Display it, and after just unset the session variable.
On the page where the comment is processed:
if($success)
{
$_SESSION['userMsg'] = "<p>Your comment has been added. Thank you.</p>";
}
In any/all pages (but mainly the one you're redirecting to):
if($_SESSION['userMsg'] != '')
{
print $_SESSION['userMsg'];
unset($_SESSION['userMsg'];
}
This is assuming you're using Sessions and have therefore previously called the session_start() function
When you redirect send via $_GET array a variable something like this:
header("LOCATION: index.php?msg=1" );
On index check if $_GET['msg']==1 then display your message
You may want to apply PRG pattern.
Basically you post the comment and the server replies to the client to perform a redirection to your page with additional info in Query string as Vadim argued.
"Elegant", sessionless and functional.

Categories