Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Page redirection with javascript

Tags:

redirect

php

a small problem ! I have a login form in a PHP file. I have used javascript validation for the form. On a login success scenario i am redirecting the user to their home page.

I have used header("Location:index.php").

I know that the header must be before any output must be sent to the browser. My question is there any walk around to do this redirection?

like image 306
anshu Avatar asked Apr 12 '13 06:04

anshu


People also ask

How to redirect a page in PHP?

For example, in PHP, the header () function is used to redirect by specifying the location URL. On the other hand, we can perform page redirects with some client-side script like Javascript. We can prefer to use Javascript redirect when we need to perform navigation on an event basis.

How to redirect a URL in JavaScript?

JavaScript is mainly client-side scripting language. Redirect operation can be done with JavaScript on the client-side by providing a new URL to the windows object’s location property like below. Even we can make a Javascript function to use it multiple times easily

How to provide a new URL to the client browser using PHP?

We will use the header function of PHP to provide a new URL to the client browser. Keep in mind that after using header function there should be no other operation that will output something or simple be sure header is the last function.

How to send a raw HTTP header in PHP?

This is an inbuilt PHP function that is used for sending a raw HTTP header towards the client. The syntax of the header () function is as follows: Also, it is likely to apply this function for sending a new HTTP header, but one should send it to the browser prior to any text or HTML.


3 Answers

You could use

<script>
    window.location = 'http://www.example.com/newlocation';
</script>

to redirect after the headers are sent.

like image 80
Tom Avatar answered Oct 09 '22 02:10

Tom


If you using javascript Use this

window.location.href = "index.php";
like image 20
chandresh_cool Avatar answered Oct 09 '22 03:10

chandresh_cool


If you are ready to use the JavaScript you can use any one of the following method.

1.window.location.assign('http://www.example.com');
2.window.location = 'http://www.example.com';
3.window.location.href = 'http://www.example.com';
like image 36
Thirumalai murugan Avatar answered Oct 09 '22 02:10

Thirumalai murugan