up vote 3 down vote favorite
share [g+] share [fb]

I've written a some PHP that interacts with a SQL database and is just around as an intermediate between two pages.

I want a user to automatically be forced to leave that page and move to another (or even a back to previous page function)

What is the easiest way to do this? I'm happy to use PHP, HTML, or Javascript to do it and if I haven't been clear enough feel free to ask more!

link|improve this question

Idont fully understand your question; pleases provide an example of what it is your trying to accomplish. Furthermore, be more specific if you cannot provide a live example. – RPM Jul 6 '11 at 4:52
i think you mean redirect? – Ibu Jul 6 '11 at 4:52
1  
Generally, you shouldn't force the end user to do anything. – alex Jul 6 '11 at 4:54
Hahaha, you know what I mean though. Re-directs are a necessary evil! – Fergus Barker Jul 12 '11 at 23:41
feedback

7 Answers

up vote 4 down vote accepted

From php you can use the header() function to change the location:

header("Location: newpage.html");

Just be aware that if you're using header, you can't emit any html or whitespace before you make this call unless you're buffering output. Doing so will cause warnings to the effect that headers have already been sent (there is a way to work around this).

link|improve this answer
That's all I needed thanks! – Fergus Barker Jul 6 '11 at 4:55
feedback

In PHP I use the following code at the end of a script to move the user to the next page

<?php
     // MYSQL SECTION
     // PHP SECTION
     header('Location: /next.php');
?>

Just make sure you haven't passed any actual HTML in the page before the header code or the script will break with an error that headers have already been sent.

I hope this helps

link|improve this answer
If you are not using a variable inside that statement, you only need single quotes; otherwise, you will need double quotes. – RPM Jul 6 '11 at 4:56
@RPM you're absolutely right, thanks for pointing that out. To clarify for others however the double quotes will still work without a variable however, if you're setting $location = '/next.php'; then the double quotes are needed in the code. Thanks again mate – Ryan Jul 6 '11 at 5:00
It's a trivial facet of the answer to the question, but I believe knowing it will be better for the long run. – RPM Jul 6 '11 at 5:05
Definitely agree mate, it is 1 of those details that help but are easy to forget and I appreciate the reminders :-) – Ryan Jul 6 '11 at 5:09
feedback

from PHP you can call JavaScript by this way

     echo "<script>document.location.href = \"yourpage.php\"</script>";

This code wont bother if there any white space or html content before the line.

link|improve this answer
feedback

in php

<?php 
  //the user must leave now
  echo"<meta http-equiv='refresh' content='".$time_in_seconds."; URL=abc.com'>";//will move after the time specified,,0 can be specified to move immediately
  exit();
?>

in js

window.location="abc.com";//will move immediately,you can time using setTimeout function
link|improve this answer
feedback

If you script just interacts with SQL database and does not output anything just simply send a redirect header.

<?php

// sql interaction code goes here.

header('Location: /uri/to/secondpage.php');
exit;
?>
link|improve this answer
1  
-1 for using double quotes. – RPM Jul 6 '11 at 5:01
+1 for pointing out. fixed. :) – Hameedullah Khan Jul 6 '11 at 5:16
+1 for fixing.. – stevether Jul 6 '11 at 5:21
+1 for fixing :) – RPM Jul 6 '11 at 12:56
feedback

you can use the php header function

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

with following example it would be more clear to you.

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>

but there is some limitation with the header function in php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

link|improve this answer
feedback

To "move" to a page in PHP you can use the header funtion.

Ex: header('Location: http://www.example.com/');

link|improve this answer
+1 for wrapping the statement in single quotes. You only need double quotes if you are using a variable inside the () – RPM Jul 6 '11 at 4:57
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.