PHP标题()不工作?
我这个问题苦苦挣扎,因为某些原因header("Location:http://corocloud.com/index.php/");
不工作,我已经尝试过其他路径的文件,但没有工作, header("Location:index.php");
,header("index.php");
,header("./index.php/");
PHP标题()不工作?
没有这些工作,我的代码是这样的:
<!DOCTYPE html>
<html>
<head>
<title>CoroCloud</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./js/material.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.pink-indigo.min.css" />
<style>.mdl-layout{align-items:center;justify-content:center;}.mdl-layout__content{padding:24px;flex:none;}</style>
</head>
<body>
<div class="mdl-layout mdl-js-layout mdl-color--grey-100">
<main class="mdl-layout__content">
<div class="mdl-card mdl-shadow--6dp">
<div class="mdl-card__title mdl-color--primary mdl-color-text--white">
<h2 class="mdl-card__title-text">CoroCloud</h2>
</div>
<div class="mdl-card__supporting-text">
<form method="POST">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" name="uname" />
<label class="mdl-textfield__label" for="uname">Username</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="password" name="pass"/>
<label class="mdl-textfield__label" for="pass">Password</label>
</div>
<div class="mdl-card__actions">
<input type="submit" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect" name="sub">
</div>
</form>
</div>
</div>
</main>
</div>
<?php
session_start();
\t // this will trigger when submit button click
\t if(isset($_POST['sub'])){
\t \t $db = new mysqli("localhost","user","password","db");
\t \t // create query
\t \t $query = "SELECT * FROM users WHERE user='".$_POST['uname']."' AND password='".sha1($_POST['pass'])."'";
\t \t // execute query
\t \t $sql = $db->query($query);
\t \t // num_rows will count the affected rows base on your sql query. so $n will return a number base on your query
\t \t $n = $sql->num_rows;
\t \t // if $n is > 0 it mean their is an existing record that match base on your query above
\t \t if($n > 0){
$_SESSION['username'] = $_POST['uname'];
$_SESSION['password'] = $_POST['pass'];
$_SESSION['userobj'] = mysql_fetch_assoc($query);
\t \t \t header("Location: index.php");
\t \t \t exit();
\t \t } else {
\t \t \t echo "Incorrect username or password";
\t \t }
\t }
?>
</body>
</html>
我知道正在执行的代码,为每$ _SESSION变量被获得价值,为什么标题不起作用?
我试图重定向的文件的方式是在同一个文件夹中。
编辑: 不要运行该代码段,因为它有PHP
由于Icewine正确指出,应在设置任何输出之前设置标题。
因此,通过在脚本的开头调用ob_start()
,将设置标题的代码移动到脚本的开头,或者通过输出缓冲捕获所有输出。
另外通常在使用位置标题重定向时不发送任何内容,另一个将重定向逻辑移动到脚本开头的原因,然后在设置标题后调用exit()
。
谢谢!我不知道在更改标题前我无法输出。作为一个事实,我有一个托管相同的代码,它的工作原理。真奇怪 –
请由ob_start()函数之前和头位置
ob_start();
header("Location:http://corocloud.com/index.php/");
exit;
我希望这将有助于试试吧!你请提供错误,你在那里得到什么。
你不能输出东西,然后在php中执行“标题”操作。 所以,修改你的代码首先做所有的PHP的东西,然后输出HTML。 在“标题”行之前,您不得有任何输出(html,警告等)。 参见:http://php.net/manual/en/function.header.php
完全击败你23秒。 – Vbudo
在你的代码的顶部HTML代码之前添加以下代码//
ob_start();
session_start();
// this will trigger when submit button click
if(isset($_POST['sub'])){
$db = new mysqli("localhost","user","password","db");
// create query
$query = "SELECT * FROM users WHERE user='".$_POST['uname']."' AND password='".sha1($_POST['pass'])."'";
// execute query
$sql = $db->query($query);
// num_rows will count the affected rows base on your sql query. so $n will return a number base on your query
$n = $sql->num_rows;
// if $n is > 0 it mean their is an existing record that match base on your query above
if($n > 0){
$_SESSION['username'] = $_POST['uname'];
$_SESSION['password'] = $_POST['pass'];
$_SESSION['userobj'] = mysql_fetch_assoc($query);
header("Location: index.php");
exit();
} else {
echo "Incorrect username or password";
}
}
// Put here html code //
更好的方法是总是试图遵循一些基本的PHP。后头
<?php
ob_start();
session_start();
出口
header("Location:demo.php");
exit;
尝试'标题( “位置:的index.php”);' 注意**的路径之间的空间**和**: ** – Dash
在调用'header'之前,你有任何输出吗? – user2890234
如果您在标题之前输出某些内容,它将不起作用 – Icewine