is there a way to make a redirection from inside a phtml file in ZF2 Framework?
The reason is that I am using the jQuery $.POST mechanism (ajax) to refresh a part of the page, but I cannot make a redirection from the controller used by the ajax because it will load the new page into the div (which is inside another page).
I have tried using ZF2's :
$this->_redirect($this->url('restaurants'));
But I get a ServiceNotFoundException. I also tried php's:
<?php header( 'Location: http://www.yoursite.com/new_page.html' ) ; ?>
But this just did not work at all.
Hope this is clear enough!.. Any help is much appreciated!
You can't use header when you printed anything before it. There must not be any output before header. So this means the exact same for $this->_redirect().
What you could do is the following:
Add this to your head section
<meta http-equiv="refresh" content="0; url=http://example.com/">
or use javascript to redirect the user. If you don't want to extend your layout file you should use javascript:
<script type="text/javascript">
window.location = "http://www.example.com/"
</script>
EDIT
After rereading your question i think the only possible way for you to redirect the user after you've received the page content is the javascript solution.
Related
I recently started building a login system using php and mysql database.
Right now I have already built this using php code and data typed by user in a bootstrap modal.
User opens his mailbox, click a link which executes verification script in database to set user as verified and load my main site page.
After that I would like to add some modal to tell something like "Your account was verified" just once. What is a right way to do it? I thought that it can be done by php by running jquery function located in index.php like
<?php
header('location:index.php');
echo "<script> $('#addText').modal();</script>";
?>
But as I can see it is not correct and php just loads index.php and that's all.
P.S. I am sorry for my ignorance of php fundamentals.
If you use an HTTP redirect, the contents of the page are ignored.
Instead, you can use a meta-refresh tag, which allows delaying the redirect.
<head>
<meta http-equiv="refresh" content="5;url=https://example.com/index.php" />
<script src="js/jquery.js">
<script> $(function() {$('#addText').modal();})</script>
</head>
5; means to delay the refresh for 5 seconds.
Aim : I have a small shortener service. I use links generated using this services as part of the content I post on facebook. Facebook's URL scraper tries to generate a preview by locating the end-point URL and pull relevant information.
This, right now works because I simply redirect in PHP with the way it's mentioned below without allowing me to execute JS before that redirection. I want to find out if there is a way to execute that JS.
What can I do? Well! redirect from JS using window.location like this solution here...
What's the problem? If I do that, Facebook's URL scraper can no longer find the final destination url, resulting in a blank preview of my site(specifically the page where JS executes) instead of the destination site. JS execution is a must so can't omit that.
Question : Is there a way I can execute some piece of javascript before I do a redirection via header location and still be able to get correct previews for URLs?
Reference Code - in Laravel Controller
if($row->save()){
//i was first doing this and doing a redirect from the view.
//return View::make('pages/redirect')->with('originalUrl',$row->url);
//but then i realized it wasn't fetching the previews. So i do this now.
return Redirect::to($row->url);
//assuming it internally uses php's header location. I want to use the JS before this redirect.
}
I have finally got around the problem by following what #Austin had mentioned in comments.
I put the script after header location statement in the view and returned that view from controller. That way facebook's scraper found both my script and the content from destination URL as a merged markup( as seen from the facebook url debugger )
I find it kinda funny and weird but it works! :D
I tried out the idea I mentioned in the comments.
It looks like it works.
<?php
if (strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit") !== false) {
// this is the facebook crawler
header('location: http://stackoverflow.com/');
}
else {
// other clients
echo '
<script>
window.location = "http://stackoverflow.com/"
</script>
Hello World
';
}
?>
I have a PHP file which deals with registration on a site, currently once the user has registered they are redirected to the home page and a success message states that the user's registration has been successful.
I would like to change this to a jquery notification.
Is it possible for me to include jquery within this registration PHP file?
For example.
<script type="text/javascript" src="js/jquery_v_1.4.js"></script>
<script type="text/javascript" src="js/jquery_notification_v.1.js"></script>
If I place this at the top of the page before the PHP tags commence.
If this is not possible, what is the best way to overcome this obstacle?
Thank you
Yes that is fine, as long as they are outside of the <?php ?> tags
Yes you can do it. just be careful not to put any javascript/html (any output, not even space) code before header or session_start functions, cause these will cause problems if anything is printed before they run.
i'm using this code to load a php page inside a html page using javascipt , but it doesn't work. the code is below :
<html>
....
<div id="home" style="background: #000;background-image: none;height:100%;"></div>
<script>
$('#home').load('http://www.website.com/file/index.php').trigger("create");
</script>
</html>
the php file exists and works fine but it doesn't show up in the actual page . If you have modification i can make i'll be thankful
If you remote website is not in same server you can't do it, for same-origin policy security restrictions, as specified in the load() documentation.
But, you will have a proxy script like:
proxyScript.php
<?php echo file_get_contents("http://www.website.com/file/index.php"); ?>
And, now, you can: $('#home').load('proxyScripts.php').trigger("create");
Or you will configure your remote server to accept remote request, read: How to use Cross domain Ajax request
I think it has something to do with javascript happening AFTER php happens.
Why not switch it with
include('http://www.website.com/file/index.php');
if your hiding that div you could just use javascript to show / hide the div based on what ever happens. Dont try to lead the php file when its hovered or clicked or w/e. Load it first and just display it with js.
I need to render a page without executing it's JavaScript (however inject my own script), showing the user how the page would look from a bot's POV.
So far I have thought of loading the page using ajax, removing all <script></script> tags from the loaded data, injecting my own <script></script> tags and replacing page html with the filtered data.
Are there any better ways of achieving this?
Maybe not a better way, but an alternative to using javascript to do what you want:
You can write a (php) server-side script, use file_get_contents() to get the original page contents, use php to remove and replace javascript page contents (str_replace, substr_replace, preg_match) then call this php script in an iframe.
See my related answer for more detail: https://stackoverflow.com/a/17262334/888177
<meta http-equiv="refresh" content="5; url=http://example.com/">
Meta refresh.
EDIT:
So, here's something you can do:
Check out this jquery plugin called fancybox.
What it does is, load remote url content into a neat popup div on the page. You can check if you can modify it's code to make it work how you want.
Also quick headsup: bots don't have cookies as well. So, stripping just script tags won't do. Also have to disable cookies in the request.