Joomla setRedirect doesn't work - redirect

I have a simple Joomla controller, but I can't redirect anything.
According to the documentation:
class MyController extends MyBaseController {
function import() {
$link = JRoute::_('index.php?option=com_foo&ctrl=bar');
$this->setRedirect($link);
}
}
//The url contains & html escaped character instead of "&"
This should work, but I get a malformed URL. Is there something I'm missing here? Why is Joomla converting all the "&" characters into &'s? How am I suppose to use setRedirect?
Thank you

Alright, I fixed it. So if anyone needs it:
instead of
$link = JRoute::_('index.php?option=com_foo&ctrl=bar');
$this->setRedirect($link);
use
$link = JRoute::_('index.php?option=com_foo&ctrl=bar',false);
$this->setRedirect($link);
to make it work.

Glad you found your answer, and by the way, the boolean parameter in JRoute::_() is by default true, and useful for xml compliance. What it does is that inside the static method, it uses the htmlspecialchars php function like this: $url = htmlspecialchars($url) to replace the & for xml.

Try this.
$mainframe = &JFactory::getApplication();
$mainframe->redirect(JURI::root()."index.php?option=com_foo&ctrl=bar","your custom message[optional]","message type[optional- warning,error,information etc]");

After inspecting the Joomla source you can quickly see why this is happening:
if (headers_sent())
{
echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
}
else
{
... ... ...
The problem is that your page has probably already output some data (via echo or some other means).
In this situation, Joomla is programmed to use a simple javascript redirect. However, in this javascript redirect it has htmlspecialchars() applied to the URL.
A simple solution is to just not use Joomlas function and directly write the javascript in a way that makes more sense:
echo "<script>document.location.href='" . $url . "';</script>\n";
This works for me :)

/libraries/joomla/application/application.php
Find line 400
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
}
replace to
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . $url . "';</script>\n";
}
This works!

Related

In(Mazento) Zend..How Can I Pass String(Some Text) from Controller to View in Redirect URL

I used Like this But This is not Working
$msg=array('id' =>'Account Sucessfully Deleted');
$this->_redirect('customer/account/dresshoheaccount',$msg);
i want $msg Print in My Page,
you can easily do this as follows :
Mage::getSingleton('catalog/session')->addSuccess($this->__('Account Sucessfully Deleted'));
Please note that insert this line before you laid your layout.
you can call this is template as :
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
If not helping, try to add messages not to catalog/session, but to core/session.

Laravel, how to use form helper outside blade?

For some reason I have to write HTML::macro() to return HTML tags.
HTML::macro('myMycro', function()
{
$result = '<form id="xxx">...';
return = $result;
}
then I can use the HTML::myMacro() inside my blade.
{{ HTML::myMacro() }}
Is it possible to use form helper Form::open(), Form::input() to generate HTML tags inside the macro so I don't have to manually write tags???
If so, please suggest me how to do it because of my poor background in PHP and Laravel, I just simply tried
...
$result = Form::open('some_parameters');
...
But I didn't work, I don't know can I use form helper outside blade or not, so please advise me.
Thanks.
I dont see any reason why not.
This works like a charm
Form::macro('myForm', function()
{
$output = Form::open(['url/to/post']);
$output .= Form::text('firstName');
$output .= Form::close();
return $output;
});
// Then use in in regular PHP view...
echo Form::myForm();
// ... or even Blade view
{{ Form::myForm() }}

How to keep Zend_Debug HTML tags out of Zend_Log

I use Zend_Debug::dump to dump variables into a Zend_Log file. How can I get it to stop wrapping the output in HTML tags?
The documentaion says "If the output stream is detected as a web presentation, the output of var_dump() is escaped using ยป htmlspecialchars() and wrapped with (X)HTML tags." Why does it think my log file is a web presentation?
The method for the dump function has a boolean $echo flag. Even when this is FALSE, I get HTML markup in my log files.
Thanks for you help!
Zend Debug is always using htmlspecialchars() to quote. You cant disable this by an provided parameter.
The boolean for "echo" is only used to disable the var_dump() (wich is used in Zend_Debug) printing to the browser.
Code from Zend_Debug::dump():
$output = htmlspecialchars($output, ENT_QUOTES);
if (self::getSapi() == 'cli') {
$output = PHP_EOL . $label
. PHP_EOL . $output
. PHP_EOL;
} else {
if(!extension_loaded('xdebug')) {
$output = htmlspecialchars($output, ENT_QUOTES);
}
$output = '<pre>'
. $label
. $output
. '</pre>';
}

Twitter RSS feed, [domdocument.load]: failed to open stream:

i'm using the following:
<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
for($i=0;$i<=3;$i++) {
$tweet=substr($arrFeeds[$i]['title'],17);
$tweetDate=strtotime($arrFeeds[$i]['date']);
$newDate=date('G:ia l F Y ',$tweetDate);
if($i==0) { $b='style="border:none;"'; }
$tweetsBox.='<div class="tweetbox" ' . $b . '>
<div class="tweet"><p>' . $tweet . '</p>
<div class="tweetdate">#' . $newDate .'</div>
</div>
</div>';
}
return $tweetsBox;
?>
to return the 4 most recent tweets from a given timeline (XXXXX is the relevant feed)
It seems to work fine but i've recently been getting the following error sporadically:
PHP error debug
Error: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway
I've read that the above code is dependant on Twitter beign available and I know it gets rather busy sometimes. Is there either a better way of receiving twits, or is there any kind of error trapping i could do to just to display "tweets are currently unavailable..." ind of message rather than causing an error. I'm usnig ModX CMS so any parse error kills the site rather than just ouputs a warning.
thanks.
I know this is old, but I was just searching for the same solution for a nearly identical script for grabbing a twitter timeline. I ended up doing this, though I haven't been able to thoroughly test it.
I defined the twitter url as a variable ($feedURL), which I also used in $doc_load. Then, I wrapped everything except for the $feedURL into this conditional statement:
$feedURL = "http://twitter.com/statuses/user_timeline/XXXXXXXX.rss"
$headers = #get_headers($feedURL);
if (preg_match("/200/", $headers[0])){
//the rest of you original code in here
}
else echo "Can't connect user-friendly message (or a fake tweet)";
So, it's just checking the headers of the the feed's page, and if its status is 200 (OK), then the rest of the script will execute. Otherwise, it'll echo a message of your choice.
(reference: http://www.phptalk.com/forum/topic/3940-how-to-check-if-an-external-url-is-valid-andor-get-file-size/ )
ETA: Or even better, save a cached version of the feed (which will also ensure you don't go over your API limit of loads):
<?php
$cache_file = dirname(__FILE__).'/cache/twitter_cache.rss';
// Start with the cache
if(file_exists($cache_file)){
$mtime = (strtotime("now") - filemtime($cache_file));
if($mtime > 600) {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
echo "<!-- twitter cache generated ".date('Y-m-d h:i:s', filemtime($cache_file))." -->";
}
else {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
//End of caching
?>
Then use $cache_file in your $doc->load($cache_file) statement instead of the actual feed url.
(Adapted from here: http://snipplr.com/view/8156/twitter-cache/).

Triggering conversion tracking code on form submit

I have a PHP form that mail()s the form data on submit and then if successful returns them to the referring page (in other words keeping them on the same page as the form) and appends ?success=TRUE to the URL.
The question is, how would I implement the AdWords and Yahoo Search Marketing conversion code snippets to trigger only when the form is submitted? For functionality purposes, it is unfortunately not feasible to send them to another page on submit which would have been the easiest way to do it.
The relevant code from the form submit action that mails the results and sends them back to the homepage is below. I have a hunch it might be as simple as outputting the conversion tracking code snippets in the if statement at the end there but I'm not sure if that is correct or the syntax to properly do that.
if ( isset($_POST['sendContactEmail']) )
{
$fname = $_POST['posFName'];
$lname = $_POST['posLName'];
$phone = $_POST['posPhone'];
$email = $_POST['posEmail'];
$note = $_POST['posText'];
$to = $yourEmail;
$subject = $yourSubject;
$message = "From: $fname $lname\n\n Phone: $phone\n\n Email: $email\n\n Note: $note";
$headers = "From: ".cleanPosUrl($_POST['posFName']. " " .$_POST['posLName'])." \r\n";
$headers .= 'To: '.$yourName.' '."\r\n";
$mailit = mail($to,$subject,$message,$headers);
if ( #$mailit ) {
header('Location: '.$referringPage.'?success=true');
}
else {
header('Location: '.$referringPage.'?error=true');
}
}
Outputting it in the if-Statement would be a possibility, but the script you posted adds another way to do it as it redirects to the $referringPage - if the mail was successfully sent. And that's the only event you want to track a conversion.
So edit the code of $referringPage (the page that holds the form fields) and add:
<?php
if($_GET['success'] == 'true') {
echo "...";
}
?>
"..." ofcourse has to be replaced by the Adwords conversion Code Google gave you.
If you add it to your question, I could even add it to my answer.