2015-05-13 109 views
0

我是一個真正的初學者,我做了一個簡單的login.php,但我想知道如何使登錄按鈕重定向到另一個頁面。該腳本我有是: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 
 
\t if(isset($msg) & !empty($msg)){ 
 
\t \t echo $msg; 
 
\t } 
 
?> 
 
<h1>Login</h1> 
 
<form action="" method="POST"> 
 
    <p><label>User Name : </label> 
 
\t <input id="username" type="text" name="username" placeholder="username" /></p> 
 
    
 
    <p><label>Password&nbsp;&nbsp; : </label> 
 
\t <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。 感謝您的任何和所有輸入!

+0

告訴我,這是一個錯字'

+0

'if(isset($ msg)&!empty($ msg)){'不會運行,語法錯誤。 –

+0

我應該在dreamweaver中使用它不會註冊,除非我刪除?它只是顯示爲帶有問號的文本。 – KSykes

回答

-1

你的舊代碼有很多錯誤。這個修復了大部分,並重定向到form.html。

順便說一句,你想線路<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 } ?> 
+0

'action =「form.html」'你確定嗎?除非OP知道如何指導Apache。 –

+0

謝謝!重定向工作,但由於form.html是在父目錄中的另一個文件夾,我會把

? – KSykes

+0

呃,只要它與'site/form.html'和'/ site/form.html'不知道哪一個:D –

0

只要把

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

在您成功登錄的代碼。

0

歡迎StackOverflow上。如果用戶沒有帳戶,您希望該按鈕將他們引導至註冊頁面。對?你可以像這樣使用javascript onclick事件。

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