我想要做的就是在用户登录后几秒钟将用户重定向到我的主页。这是我的代码
<?php
include_once("config.php");
?>
<?php if( !(isset( $_POST['login'] ) ) ) { ?>
<!DOCTYPE html>
<html>
<head>
<title>Codecall Tutorials - Secured Login with php5</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header id="head" >
<p>Codecall tutorials User Login</p>
<p><a href="register.php"><span id="register">Register</span></a></p>
</header>
<div id="main-wrapper">
<div id="login-wrapper">
<form method="post" action="">
<ul>
<li>
<label for="usn">Username : </label>
<input type="text" maxlength="30" required autofocus name="username" />
</li>
<li>
<label for="passwd">Password : </label>
<input type="password" maxlength="30" required name="password" />
</li>
<li class="buttons">
<input type="submit" name="login" value="Log me in" />
<input type="button" name="register" value="Register" onclick="location.href='register.php'" />
</li>
</ul>
</form>
</div>
</div>
</body>
</html>
<?php
} else {
$usr = new Users;
$usr->storeFormValues( $_POST );
if( $usr->userLogin() ) {
echo "Welcome";
} else {
echo "Incorrect Username/Password";
}
}
?>
此外,在用户注册后创建用户页面会非常好。我有所有设置的注册码,但这是我想要实现的。有什么想法吗?
答案 0 :(得分:1)
这段代码:
header( 'Location: http://www.yoursite.com/new_page.html' );
以下是您需要的参考链接 - http://php.net/manual/en/function.header.php
答案 1 :(得分:1)
以下是PHP重定向的方法:
header('location:index.php');
答案 2 :(得分:1)
尝试添加
header();
phpfunction将您的网页重定向到您想要的特定位置。
if( $usr->userLogin() ) {
ob_start(); // use output buffering to avoid "header already sent error"
echo "Welcome"; //should try to remove this if you want because its unecessary now since your redirecting your page
header('Location: pagetoredirect');
ob_end_flush(); //now the headers are sent
} else {
echo "Incorrect Username/Password";
}
源(PHP.NET)
答案 3 :(得分:0)
您需要先清除您的HTML:
将您的顶级代码编入:
<?php
include_once("config.php");
if( !(isset( $_POST['login'] ) ) ) { ?>
将header
添加到您要重定向的位置:
if( $usr->userLogin() ) {
header('Location: /path/to/new/page.php');
} else {
echo "Incorrect Username/Password";
}
另一种方法是使用javascript:
if( $usr->userLogin() ) { ?>
<div class="message">
Thank you for logging in. You will be redirected in few seconds.
Click <a href="/path/to/new/page.php">here</a> if not redirected.
</div>
<script type="text/javascript">
window.location = "/path/to/new/page.php";
</script>
<?php
} else {
echo "Incorrect Username/Password";
}
答案 4 :(得分:0)
您可以通过标题()重定向您的网页
header("location:yoursite.com?$msg=welcome user");
重要的是要注意在看到任何实际输出之前必须调用header()...