Set header redirect after page load - php

Is it possible to redirect the user to the correct page after headers have been set?
I have developed a plugin in WP that allows users to manage content they have created on the site... for example, they can delete it or disable comments.
The author options appear at the top of the authors post... When the user clicks the button to delete their post, it changes a value in the db to soft delete it:
$result = $this->db->update(
$this->_table,
array('active' => $status), // 0 / 1
array('id' => $id)
);
if($result > 0)
{
$this->setMessage('success', 'Post deleted.');
}
else
{
$this->setMessage('error', 'Some error.');
}
The page appears to refresh as the message is shown, but the post is still showing.. the page must be refreshed again before a 'this post does not exist message' appears.
I have tried lots of WP hooks to try and fire wp_redirect before the header but i'm always getting an 'headers already sent' error.
I know I am using WP but I believe this is a http/php limitation so I decided to post it here.
I wanted to do something like:
if($result > 0)
{
header('Location: ...');
}
else
{
$this->setMessage('error', 'Some error.');
}
Using output buffering is not an option.
This type of redirecting is available in Laravel and you can even pass a message variable to be shown on the next page so it must be possible some how.

Simple and working solution using javascript. Used this type of reirection in my projects. No problems ever :)
if($result > 0)
{
echo "<script>parent.self.location='ENTER YOUR URL HERE';</script>";//enter location..example www.google.com, or profile.php etc
}
else
{
$this->setMessage('error', 'Some error.');
}

You can use the javascripts
window.location.href='enter_your_url_here.php';

There's two ways to do that,
First one using Javascript as #Unni Babu and #ash_8247 answer.
Second way is to use a buffer
//allow redirection, even if my theme starts to send output to the browser
add_action('init', 'do_output_buffer');
function do_output_buffer() {
ob_start();
}
See this post

Related

GET variable from url is ignored when after folder-like page

At work (well more like an apprenticeship, dunno) I'm making a wordpress plugin that allows users to submit their posts (it's for a website catalog) with the ability to pay for some premium features. For this I'm using PayU.
I have a shortcode which I put on a page where users end up after the payment, and the shortcode shows appropiate message depending on GET variables. Eg. "your post awaits moderation" with a free post, "payment was successful" when it was so etc.
So when payment in PayU fails it will stick &error=501 to the end of the url, so my code detects that and tells the user their payment failed and their post won't be submitted. And here's the thing.
When the confirmation page url is itself a variable, eg. ?post_id=71, everything works as expected. So for mywebsite.com/?post_id=71&posttype=paid&error=501 the error is read and shortcode acts appropiately. But when the address in folder-like (sorry, really dunno how to call that) eg. /confirmation, error variable gets unnoticed. So with an address like mywebsite.com/confirmation?posttype=paid&error=501 the result is a "payment successful" message. Order of variables doesn't seem to matter, posttype is always interpreted and error is always omitted.
I don't have access to the code right now but it's basically this:
function my_shortcode() {
if(!isset($_GET['posttype'])) {
// some generic text to handle this unexpected condition
}
else if($_GET['posttype'] == 'free') {
// nice message on how the post awaits moderation
}
else if($_GET['posttype'] == 'paid') {
if(isset($_GET['error'])) {
if($_GET['error'] == '501') {
// payment was unsuccessful
} else {
// some generic text cause this isn't supposed to happen too
}
} else {
// payment was successful
}
}
}
As I said, I'd expect the error value to modify the behavior accordingly, instead it only does so when page address is itself a query.

How can I persist data for a redirect with CodeIgniter?

I'm attempting to validate a users login attempt and inform them that
Their username is wrong or
their password is wrong (because I personally hate with a blind fury when a website doesn't inform me WHICH it is but that's beside the point).
I've read a lot of SO posts on this issue but the ones I've found are years old and I'm dealing with CodeIgniter 3.0.1.
This is the code that I have in place. I'm using Eclipse PDT to as my IDE and I like it quite a bit (but that's getting off track) so I've been able to step through the execution and watch as it just fails completely.
IF (!$this->User->login( //Testing shows this works fine - the SWITCH statement gets executed as it should and the BADUSERNAME case is followed through.
addslashes(strtolower($this->input->post('username', TRUE))),
addslashes($this->input->post('password', TRUE)),
$this->getIP())){
SWITCH($this->User->ID){
CASE 'BADUSERNAME':
$this->session->set_flashdata('user_msg', 'Invalid Username');
BREAK;
CASE 'BADPASSWORD':
$this->session->set_flashdata('user_msg', 'Invalid Password');
BREAK;
CASE 'ALREADYLOGGEDIN':
$this->session->set_flashdata('user_msg', 'You are logged in elsewhere.');
BREAK;
DEFAULT:
$this->session->set_flashdata('user_msg', 'Something has gone terribly wrong. Please try logging in again.');
BREAK;
}
redirect(base_url());
}
Then a bit further down I load the header, body, and footer views - The body is where the error message should be displayed but it's not..
<div id="contentarea">
<div class="container">
<?PHP
ECHO $this->session->flashdata('show_validation') ? validation_errors() : '';
$error = $this->session->flashdata('user_msg'); //This is where it's supposed to get it...
IF ($error) //And this is where it's supposed to show it...
ECHO "<div class='error'>$error</div>";
?> //But the value is wiped so it only ever grabs NULL.
I've followed the path of execution after calling the redirect after setting the flash data and I've noticed that after the redirect finishes it's chain of execution, it calls exit;.
Then everything loads again from the index.php file, and when Session finally pops up... the value 'user_msg' is nowhere to be found.
So clearly I'm doing something wrong here - what am I doing wrong here? Will the flash_data only persist until that redirect is called? Even the session_data values (calling $this->session->value = 'some arbitrary user message' fails to persist).
How can I persist the message for the next time the body element is loaded so that it can tell the user "Hey, didn't find you" or "Hey, your password wasn't right"?
EDIT 1
So it turns out I do not need to redirect for what I am doing as POSTing (submitting the user name and password) handles that for me.
I'm going to leave the question here for anyone else who may need it answered though - perhaps the answer is simply that Flash data just doesn't survive a redirect?
Flashed data is only available for the next http request, if you reload the page a second time, data is gone.
To persist data in the session, you want to set the variable in the session.
Codeigniter
Adding Session Data
Let’s say a particular user logs into your site. Once authenticated, you could add their username and e-mail address to the session, making that data globally available to you without having to run a database query when you need it.
You can simply assign data to the $_SESSION array, as with any other variable. Or as a property of $this->session.
Alternatively, the old method of assigning it as “userdata” is also available. That however passing an array containing your new data to the set_userdata() method:
$this->session->set_userdata($array);
$this->session->set_userdata('username', 'username is wrong');
in the view
$this -> session ->userdata('username');
or
$this ->session -> username;
Reference Session Library Codeigniter.
hope this help.
All you have to do is use $this->session->keep_flashdata('user_msg') with $this->session->unset_userdata('user_msg')
here is the solution (view file)
<?php
$error = $this->session->flashdata('user_msg');
if (isset($error)) {
echo '<div class="error">' . $error . '</div>';
$this->session->unset_userdata('user_msg');
}
?>
After that in your controller construct function (In that controller where you redirecting)
public function __construct() {
parent::__construct();
//.....
$this->session->keep_flashdata('user_msg');
}
I had same problem and this works. do not forget to clear cache when try or try in different browser
You can use codeigniter's flashdata to display errors separately.
This is what I usually use.
Controller:
$errors = array();
foreach ($this->input->post() as $key => $value){
$errors[$key] = form_error($key);
};
$response['errors'] = array_filter($errors);
$this->session->set_flashdata($response['errors']);
redirect('your-page', 'refresh');
And the to display the errors use
<?php echo $this->session->flashdata('field_name'); ?>

Abort running WordPress function under certain conditions

I am trying to figure out how to abort a running WordPress function. When the user deletes a custom post type (in my case, a store), I want to check to see if there are any associated posts with that store. I am running a query and checking to see if there are returned results. If we return 1 or more results, I want to abort the delete and present the user with an error message stating that they must delete the associated post. I am using the action 'before_delete_post'. Here is what I'm going for:
if (count($results)==0){
//delete the data
} else {
//abort the delete.
}
Thanks in advance for the assistance.
if you are using before_delete_post you could have something like this:
function prevent_delete_custom_post() {
if (count($results)==0){
//delete the data
} else {
wp_redirect(admin_url('edit.php')); //here you can try to get the variables that you have in the url to redirect the user to the same place.
exit();
}
}
add_action('before_delete_post', 'prevent_delete_custom_post', 1);
Remember that 'before_delete_post' action is fired before post metadata is deleted.
http://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post
And 'delete_post' action is fired before and after a post (or page) is deleted from the database.
http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post

Page reloading before it know the post has been deleted?

I have a class that contains 2 functions one for removing post and one for submitting post. The function for submitting the post will redirect to any page I want just fine and if it is the same page it will be displayed with the new post.
The function used for removing the post will refresh or redirect but if it's redirected back to the same page the page does not appear updated until the page is refreshed again (a second time). I have looked and haven't found anything that works or a reason why this is happening. I'm new to php so go easy on me!
Is the page reloading before it know the post has been deleted? What can I do to solve this?
Here is my code:
class userPost{
// For Removing Post
function remove(){
if(($_SERVER['REQUEST_METHOD'] == "POST") && ($_POST['delete_id'])){
if(!$_POST['delete_id'] || !$_SESSION['SESS_USER']){
header('Location: ?id=profile');
}
else{
$delete = mysql_real_escape_string($_POST['delete_id']);
$remove = mysql_query("DELETE FROM Post WHERE post_id = '".$delete."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($remove){
header('Location: ?id=profile');
}
elseif(!$remove){ ?>
<script>
$('#div-id').triggerevent(function(){
$('#div-id').html(newContent);
});
</script>
<?php
} // End Else
} // End Else
} // End If
} // End If
} // End Class
You need an exit; after header. Otherwise PHP continues to process the script.
As an aside: Don't continue programming this way. There are so many issues with this script I wouldn't know where to start. A good book to learn PHP the right way is PHP : Ojects, Patterns, and Practices by Matt Zandstra.

When form validation fails, how to pass error information for new try?

I'm developing a form validation class in PHP. When form validation fails, I can easily redirect again to the form's html page but without error information. I would like to redirect to the form's page with the specific errors about which fields failed and why.
How should I do this? Should I send information back via GET or POST? and in which format? Would really to see some code to see how people tackled this problem.
Thanks!
You could use the header() function. So just check the fields that are posted:
if(!$form->valid()){
$msg = "Form is not valid";
} else {
//Don't know if you want this
$msg = "Form is valid";
}
header("Location: [page where you came from]?msg=" . urlencode($msg));
Then in the page where you're redirecting to use
if(isset($_GET['msg]))
echo urldecode($_GET['msg']);
to echo the message. If you are using other get variables in the location of the header function, of course, use &msg=" . urlencode($msg). (You may also want to return the values that the user submitted, so the user doesn't have to fill out the entire form again if he makes 1 mistake.
I agree with user187291's suggestion of using $_SESSION because:
It doesn't hijack the URI like using $_GET (you would never want a static link to a status message). Users could press "back" to the page with your form and still see a status message because the URI says so.
Print and unset it in the same run, you won't be able to use it more than once (which is what you want?!)
If you're going with AJAX, $_GET is more widely used for retreiving values, which you are doing from the validation controller.
there are number of approaches
pass errors in GET when redirecting back like you said
use sessions to store error info, on the form page check Session for errors
do not redirect after failure, just output form again along with error messages
ajax submits
which one to use depends on the application. For most apps sessions method is most appropriate.
Something like this:
// Pseudo Code
function isValid($parm) {
$return = false;
if(preg_match(/^[a-zA-Z]+$/, $parm) {
$return = true;
}
return $return;
}
$firstname = $_GET["fname"];
$lastname = $_GET["lname"];
$validFirstName = isValid($firstname);
$validLastName = isValid($lastname);
if($validFirstName == true && $validLastName == true) {
echo "It's all good";
// Do what you need to like, Submit
} else {
echo "Please retry";
// Display error message
}
I use a class to interface with $_POST, similar to the following:
// create the object
$post = new PostData();
// register your requirements... write whatever methods you need
// for each call,read $_POST, check criteria, and mark the field
// as good or bad...
$post->required ('LastName');
$post->required ('FirstName');
$post->numeric ('Age');
$post->optional ('MiddleInitial');
$post->regExp ('/\d{3}/','AreaCode');
$post->email ('Email');
// check the status
if (!$post->isValid ())
{
$_SESSION['FailedPostData'] = $post;
header ('Location: page.php');
}
// normal form processing
Then, on page.php, you can see if FailedPostData is in the session, read it to find the info entered last time, as well as which fields that failed. I use a template engine with macros that let me easily re-populate the form inputs and mark the failures. Otherwise you might end up with lots of code for a simple form...
You'll also need a mechanism to be sure that stale FailedPostData doesn't hang around in the session and confuse things.
I am doing it this way. Beginner in php so not sure if this is the best way to do:
HTML Form Page:
<form id="abc" method="post" action="validate.php">
PHP Page
..validation conditions..call a function if things do not match
function display_error($error) {
echo "<html><body><link href='style.css' rel='stylesheet' type='text/css'><br><center><h2>";
echo "$error";
echo "</h2></center><br><br>";
echo "<center><input type='button' value='Go Back' onClick='history.go(-1)' style='width:100px; height:28px; font-size:16px'></center>";
echo "</body></html>";
}
Clicking on the back button takes you back to the html page with the data intact.

Categories