Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

php - PHP标题(位置:...):在地址栏中强制更改URL(PHP header(Location: …): Force URL change in address bar)

I'm currently working on a mobile site with authentication using PHP sessions with a database.

(我目前正在使用PHP会话和数据库进行身份验证的移动站点上工作。)

I have a login page with a form that goes to server_login.php on submit.

(我有一个登录页面,其表单在提交时转到server_login.php 。)

The php file then creates some session data (store in $_SESSION), and redirects the user back to the index page:

(php文件然后创建一些会话数据(存储在$ _SESSION中),并将用户重定向回索引页面:)

header("location:../../index.php");

The new web page (index.php) loads correctly;

(新网页(index.php)正确加载;)

however, when the header redirects the page, the URL at the address bar is not changed;

(但是,当标题重定向页面时,地址栏中的URL不会改变;)

it stays at *http://localhost/php/server/server_login.php* instead of http://localhost/index.php and thus all my other resources that makes use of relative pathing could not be loaded.

(它保留在* http://localhost/php/server/server_login.php*而不是http://localhost/index.php ,因此无法加载使用相对路径的所有其他资源。)

It's as if the web page still thinks that it resides at /php/server instead of /.

(这就好像网页仍然认为它位于/ php / server而不是/。)

Strangely, my other use of header("location: ...") at logout.php works and redirects the page successfully with a URL change.

(奇怪的是,我在logout.php上使用标题(“location:...”)可以正常工作,并通过URL更改成功重定向页面。)

I've made sure that there are no outputs in my *server_login.php* before the header redirect (above it are just mysql calls to check) and I've used ob_start() and ob_end_flush() too.

(我已经确保在我的* server_login.php *中没有输出在头重定向之前(上面只是要检查的mysql调用)并且我也使用了ob_start()和ob_end_flush()。)

Are there any methods of forcing the URL on the address bar to change (and thus hopefully fix the relative path problem)?

(是否有任何强制地址栏上的URL更改的方法(因此希望修复相对路径问题)?)

Or am I doing something wrong?

(或者我做错了什么?)

P/S: I am using jQuery Mobile.

(P / S:我正在使用jQuery Mobile。)

EDIT: Here's my code for the redirection that doesn't change the URL:

(编辑:这是我的重定向代码,不会更改URL:)

// some other stuff not shown


$sql = "SELECT * FROM $user_table WHERE email = '$myemail' AND password = '$mypassword'";
$login_result = mysql_query($sql, $connection);

$count = mysql_num_rows($login_result);

if ($count == 1) {

    // Successfully verified login information

    session_start();

    if (!isset($_SESSION['is_logged_in'])) {
        $_SESSION['is_logged_in'] = 1;
    }

    if (!isset($_SESSION['email'])) {
        $_SESSION['email'] = $myemail;
    }
    if (!isset($_SESSION['password'])) {
        $_SESSION['password'] = $mypassword;
    }

    // Register user's name and ID
    if ((!isset($_SESSION['name'])) && (!isset($_SESSION['user_id'])))  {
        $row = mysql_fetch_assoc($login_result);
        $_SESSION['name'] = $row['name'];
        $_SESSION['user_id'] = $row['user_id'];
    }

    header("Location: http://localhost:8080/meet2eat/index.php");

} else {
    // Not logged in. Redirect back to login page
    header("Location: http://localhost:8080/meet2eat/php/login.php?err=1");

}
  ask by vemoxy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try changing:

(尝试改变:)

header("Location : blabla")
                ^
                |
           (whitespace)

To

(至)

header("Location: blabla")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...