Let me try and explain as best as I can,
I have the following code in my login.php:
if(isset($_SESSION['user'])!="")
{
header("Location: home.php");
}
This ensures that if a user has logged in successfully, the url will always display mysite.com/home.php on all pages, this works fine for all php redirects using href="another_page".
However this does not work for javascript redirects using window.top.location.href="another_page", this instead redirects with the full url name (eg: mysite.com/java_redirected_page) instead of mysite.com/home.php. How can I get javascript redirects to work with headers as well, just like php does.
use this - window.location.href = 'home.php'; rather than window.top.location.href = 'home.php';
JavaScript is client side interpreter, it can't change headers from your server, while PHP generates responses from your web server, like Apache or nginx.
So the answer is that you can't change the way javascript redirects.
It looks like you want to have a nice result with your url, the only thing i can think of is MVC.
Never heard of it? Read this link http://code.tutsplus.com/tutorials/mvc-for-noobs--net-10488
Its just a nice way to seperate your Backend from your Frontend and with that your redirects would work nicely.
Sad part about is, you would have to rethink yoru whole page, which is pretty timeconsuming because you would have to restructre everything.
Hope it helps
Related
hello guys please help
How to redirect a page using php?
i am using this one
header('location:quizsection.php?success=success');
It is working on localhost
but after hosting my website it is not working
it is working on perfectly when i using with local server it is not working after i am hosting this on godaddy
and
path is also pefect
Try to use:
header('location: /quizsection.php?success=success');
When I redirect, I go overboard to ensure redirection takes place. I do this:
$url = "quizsection.php?success=success";
header('Location: '.$url);
die("<html>
<meta http-equiv='refresh' content='0; $url'>
<body onload=\"location.replace('$url');\">
<a href='$url'>$url</a>
</body></html>");
Notice that I have a space after "Location:", which you do not have. If the header is ignored, I send HTML. It includes a meta tag to redirect. If that is ignored, the page uses Javascript to redirect. If that is ignored, the user sees the URL and nothing else. The user can click on it to get to the URL.
I actually have this in a function called redirect, so I just use:
redirect("quizsection.php?success=success");
If this doesn't work, it isn't a redirection problem. You need to check your directories and files. Something is not where you think it is or is not named what you think it is. I've personally spent days working on problems that were caused by a misnamed directory or file.
When you print something in PHP, it changes that into HTML and that's what the file shows. So when you think about it you have to do the following:
print("<script type=text/javascript>location.href = \"quizsection.php?success=success\";</script>");
Tell me if this works! If you want to delay the redirect do this:
print("<script type=text/javascript>function redirect() {location.href = \"quizsection.php?success=success\"} setTimeout(redirect, amountOfMilliseconds)</script>");
FYI, there are 1000 milliseconds in a second. Let me know if this works!
I have recently moved from localhost to my live website. I have a simple PHP login page. After the users details are checked with my mysql table and if the username exist and they provide a valid password for that username they are redirected to the home page. I was using the header function built in to PHP but this seems to be no longer working now that I am on my live website.
After further research it seems that I cannot use header tags after the page loads. So I am not echo'ing out script tags to window.location redirect to my homepage. The problem with this is that when the new location is being redirected to it appears to look like an additional web pages is loading in before my actual webpage that I'm wanting to go to loads in (This is probably due to the fact that I have extra error handling code that is running after the fact). Does anyone else experience this when using this method? Is there a better way to handle this?
This is what I'm using currently just in case there is any confusion
echo '<script>window.location.href = "home.php";</script>';
I was using the header function built in to PHP but this seems to be
no longer working now that I am on my live website.
Headers must be the very first thing that your script outputs or they won't work. PHP has a convenience feature called output buffering which temporarily holds back the output until the script is done. This allows you to put calls to header() anywhere in your code, and then PHP will automatically take care of moving headers to the front of the output for you. Thus, if you have output buffering enabled, you can put headers anywhere and it will still work. If you do not have output buffering enabled, you must put headers at the very start.
I will wager that you are generating output before the headers, and your local PHP install has output buffering enabled, while your remote host does not. You can fix this in one of two ways:
Update your code to ensure that nothing is output before calls to header().
Enable output buffering on your host by setting output_buffering = 1 in your php.ini.
I recently learnt you can do this with a meta tag:
<meta http-equiv="refresh" content="2;url=http://example.com/" />
I am trying to scrape a website 'http://www.motorsingh.com/red-olx-cars-in-gulabpura'.
But this does a javascript redirect to 404 after the page is loaded with a 200 success code.
How can I get this final redirected url using PHP Curl?
PS: Have tried CURLOPT_FOLLOWLOCATION. Dint work.
You can't get a JS redirect purely in curl - you will need a JS interpreter to go further.
There are some horrendous half-working hacks, like regexing out a URL from the page contents, but it's a bad idea and difficult to maintain.
I need to make it so that when users visit a web page like example.com/news it automatically brings them to a different website like cnn.com without them having to click anything. It would be preferable if they would not even see the original web page and it would bring them directly to the other site (cnn.com in this case) I think I can use the onload event in html but I have little experience in javascript and don't know what code to use in order to accomplish this task. Thank you!I do not want to use jquery if possible.
Just one line of code (inside script tags)
<script>
window.location.href = "http://exampleurl.com";
</script>
You would be better off using headers. Depends what server side scripting language you are using. For PHP you would have the following:
header('Location: http://www.example.com/');
I have a link to my site on a very popular but now dead website. The link is incorrect, so I'd like to redirect users to the correct page when they visit. I'm using Wordpress for my content.
The problems lie in the fact that the incorrect URL that the website lists is http://www.senntenial.com/#!mysterium/cj6d . From my research, I cannot do a 301 redirect in my htaccess, since this URL has a # sign, and is client only.
So, I tried creating the following script in my header.php file.
if (window.location == 'http://www.senntenial.com/#!mysterium/cj6d'){
window.location = 'http://www.senntenial.com/mysterium/';
}
I would think this would have worked, but it doesn't. To identify the problem, I tried simply inputting window.location = 'http://www.senntenial.com/mysterium/'; Which worked correctly, meaning my problem lies in identifying the current window location.
How can I accomplish this? I assume Wordpress has a funky way of going about things with URL's to make them easier to read that may be messing with my code.
(PS, I tried WP extensions like EPS 301 redirect to no avail.)
Also, the mysterium page is static HTML, and not involved with Wordpress' CMS.
There's window.location.hash which gives the fragment part of the current URI. So something like
if(window.location.hash=="#!mysterium/cj6d"){
window.location = 'http://www.senntenial.com/mysterium/';
}
Might work. You could also use window.location.href which contains the full URI but then you need to check with and without www.