2012-09-20 157 views
2

我知道一點點的php-mysql和web編程。對於我的項目工作,我做了一個登錄模塊,它包含兩個頁面,但它有一些錯誤。我使用WAMP進行項目,只要我提供數據並按下登錄,它就不會進入profile.php頁面,並且會話不會啓動。它顯示登錄按鈕後按相同的頁面(login.php)最近兩天我正在尋找這個錯誤,但我無法解決它。我的項目有其他幾頁。這是我的代碼。

的login.phpphp登錄模塊不起作用

<html> 
<head> 
<link rel="stylesheet" type="text/css" href="text.css" ></link> 
<title>Login</title> 
</head> 

<body> 
<div class="container" > 
<div id="header" style="text-align: center;" > 
<table width="500" align="center"> 
    <tr > 
    <img src="logo.jpg"/> 
    </tr> 
</table> 
</div> 

<div> 
<table class="form" align="center" style="height:200px;padding-left:30px;float-align:center;margin-left=20px;margin-top:40px;margin-bottom:20px;text-align:;padding-left:20px;"> 
<form action="loginproc.php" method="POST"> 
<tr> 
    <td align="center"colspan="780px" > 
    <h3 style="font-family:Calibri; font-size: 22pt;font-weight: bold; color:#3B8C8C;text-align: center;">LOGIN</h3></td> 
</tr> 
<tr> 
    <td>Email ID</td> 
    <td><input type="email" name="emailid" maxlength="50" /></td> 
</tr> 
<tr> 
    <td>Password</td> 
    <td><input type="password" name="pwd" /></td> 
</tr> 
<tr> 
    <td colspan="3" align="center" style="margin-bottom:20px;"> 
    <input type="submit" name="submit" class="button" value="Login" style="margin-bottom:20px;margin-top:20px;"/> 
    <input type="reset" name="reset" class="button" value="Reset" style="margin-bottom:20px;margin-top:20px;"/> 
</tr> 
</form></table></div> 
</div> 
</body> 

</html> 

loginproc.php

<?php 
if(isset($_REQUEST['submit'])) 
{ 
$server="localhost"; 
$user="root"; 
$password=""; 
$db="utility"; 
$con = mysql_connect($server,$user,$password); 
if(!$con) 
{ 
    die("Cannot Connect".mysql_error()); 
} 
else 
    { 
    if(!mysql_select_db($db,$con)) 
    { 
    header('Location: login.php'); 
    } 
    else 
    { 
    $email = isset($_POST['emailid'])? $_POST['emailid']:""; 
    $email = mysql_real_escape_string($email); 

    $password = isset($_POST['pwd'])? $_POST['pwd']:""; 
    $password = mysql_real_escape_string($password); 

    $access = "SELECT * FROM register WHERE (email = '$email') AND (password = '$password')"; 
    $access = mysql_query($access); 
    $accessrow = mysql_num_rows($access); 

    if($accessrow == 1) 
    { 
     include('session.php'); 
     $_SESSION['email'] = $email; 
     header('Location: profile.php'); 
    } 
    else 
    { 
     header('Location: home.php'); 
    } 
    mysql_close($con); 
    } 
} 
} 
?> 
+0

既然你說,它顯示「的login.php」,這不是應顯示兩個頁面中的一個,我說你的重定向不工作。這似乎是可能的,如果連接失敗(或者如果你的代碼是壞的)。 –

+0

雅我有同樣的疑問,重定向不起作用,但在註冊模塊中使用相同樣式的代碼,並且它完美地工作。代碼中的會話管理是否有錯誤? –

+0

如果用戶沒有登錄,home.php是否會重定向到login.php? –

回答

0

在你的代碼貼出來,你有一個領先的空間,這將防止從開始以來的會話cookie具有會話在任何輸出之前設置。

*SPACE*<?php 
if(isset($_REQUEST['submit'])) 
{ 

是這個空間在您的文章有誤,或者是你真的在你的login.php文件?

+0

對不起,這是我的文章中的錯誤,實際的文件不包含空間 –

+0

您的PHP錯誤日誌有任何錯誤,以響應您的表單提交? – jimp

0

檢查數據庫連接和這一行if(!mysql_select_db($db,$con)) 就這麼你知道,使用MySQLi或PDO_MySQL代替MySQL作爲它的棄用。

+0

我檢查了數據庫連接其工作正常。註冊模塊(register.php&dataentry.php)使用相同類型的代碼。現在註冊模塊工作正常,Data成功添加到數據庫。 –

+0

嘗試更改'include('session.php');'包含'session.php';'' –

0

你的代碼在這裏

if(!mysql_select_db($db,$con)) 
{ 
header('Location: login.php'); 
} 

執行可能給定的數據庫不存在,這就是爲什麼login.php頁面showing.Check數據庫。

並嘗試妥善安排您的HTML代碼。您在Table內使用Form

它應該是這樣的

<form> 
    <table> 
     // your code.. 
    </table> 
</form>