最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHPHTMLJavascript: Automatically leave page? - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Nov 5, 2021 at 6:34 Nimantha 6,4866 gold badges31 silver badges76 bronze badges asked Jul 6, 2011 at 4:50 Fergus BarkerFergus Barker 1,3406 gold badges16 silver badges26 bronze badges 3
  • Idont fully understand your question; pleases provide an example of what it is your trying to acplish. Furthermore, be more specific if you cannot provide a live example. – Ryan Commented Jul 6, 2011 at 4:52
  • 1 Generally, you shouldn't force the end user to do anything. – alex Commented Jul 6, 2011 at 4:54
  • Hahaha, you know what I mean though. Re-directs are a necessary evil! – Fergus Barker Commented Jul 12, 2011 at 23:41
Add a ment  | 

7 Answers 7

Reset to default 4

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).

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

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

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.

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;
?>

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.

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./');
?>

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 mon 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.

in php

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

in js

window.location="abc.";//will move immediately,you can time using setTimeout function
发布评论

评论列表(0)

  1. 暂无评论