重定向

时间:2015-05-13 01:46:17

标签: php html

我是一个真正的初学者,我做了一个简单的login.php,但我想知道如何使登录按钮重定向到另一个页面。我的脚本是:

<php
//Start the Session
session_start();
   
require('connect.php');

//3. If the form is submitted or not.
//3.1 If the form is submitted
if (isset($_POST['username']) and isset($_POST['password'])) {
    //3.1.1 Assigning posted values to variables.
    $username = $_POST['username'];
    $password = $_POST['password'];

    //3.1.2 Checking the values are existing in the database or not
    $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
 
    $result = mysql_query($query) or die(mysql_error());
    $count = mysql_num_rows($result);

    //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
    if ($count == 1) {
          $_SESSION['username'] = $username;
    } else {
        //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
        echo "Invalid Login Credentials.";
    }
}

//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    echo "Hello " . $username . "
    ";
    echo "This is the Members Area
    ";
?>
<!DOCTYPE html>
 <head>
<title>Test Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!-- Form for logging in the users -->

<div class="register-form">
<php
	if(isset($msg) & !empty($msg)){
		echo $msg;
	}
 ?>
<h1>Login</h1>
<form action="" method="POST">
    <p><label>User Name : </label>
	<input id="username" type="text" name="username" placeholder="username" /></p>
 
     <p><label>Password&nbsp;&nbsp; : </label>
	 <input id="password" type="password" name="password" placeholder="password" /></p>
 
    <a class="btn" href="register.php">Signup</a>
    <input class="btn register" type="submit" name="submit" value="Login" />
    </form>
</div>
<php } ?>

我希望它重定向到的页面位于父目录中的被调用site / form.html中。 感谢您的所有输入!

3 个答案:

答案 0 :(得分:0)

刚刚放

    header('Location: http://  URL to the Page You Want  /');

您的成功登录代码在哪里。

答案 1 :(得分:0)

欢迎使用StackOverflow。如果用户没有帐户,您希望按钮将其指向注册页面。对?您可以像这样使用javascript onclick事件。

 <button class="btn" type="button" onclick=window.parent.location.href='register.php' target='_parent'>Sign Up!</button>

答案 2 :(得分:-1)

你的旧代码有很多错误。这个修复了大部分内容,并重定向到form.html。

BTW,你想要的一行是<form action="form.html" method="POST"> 好的完整代码:

<?php  //Start the Session
session_start();
require('connect.php');
if (isset($_POST['username']) and isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
    $result = mysql_query($query) or die(mysql_error());
    $count = mysql_num_rows($result);

    if ($count == 1){
        $_SESSION['username'] = $username;
    }

    else{
        echo "Invalid Login Credentials.";
    }
}
if (isset($_SESSION['username'])){
    $username = $_SESSION['username'];
    echo "Hello " . $username;
    echo "This is the Members Area";

?>
<!DOCTYPE html>
 <head>
<title>Test Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!-- Form for logging in the users -->

<div class="register-form">
<?php
    if(isset($msg)){
        echo $msg;
    }
 ?>
<h1>Login</h1>
<form action="form.html" method="POST">
    <p><label>User Name : </label>
    <input id="username" type="text" name="username" placeholder="username" /></p>

     <p><label>Password&nbsp;&nbsp; : </label>
     <input id="password" type="password" name="password" placeholder="password" /></p>

    <a class="btn" href="register.php">Signup</a>
    <input class="btn register" type="submit" name="submit" value="Login" />
    </form>
</div>
<?php } ?>