PHP Developer Channel :: 討論園地

您尚未登入。

#1 2005-04-07 13:32:11

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

請問關於一個簡單的測試登入註冊系統

是這樣的,由於參考了網路的教學後,寫了一個很簡單的登入註冊系統(測試版),

總共包含了index.html, register.html, not.html, login.php, create_entry.php, 以及底下的一個目錄test和裡面的index.html

這個系統就像大多數基本的論壇裡, 想要瀏覽test檔案夾內的index.html的話就要先登入,

這是測試用的系統網址 http://pinwith.no-ip.com/test_login01/

但是總會有問題,以下為login.php和create_entry的coding:

<!-- login.php -->
<?
if ($submit = "Login")
{

  mysql_connect("localhost", "root") or               
    die ("Could not connect to database");

  mysql_select_db("user") or
    dir ("Could not select database");   

$result=mysql_query("select * from user where username='$username'")
             or die ("cant do it");

while ($row=mysql_fetch_array($result)) {
if ($row["password"]==$password )
  {
    header("Location: test/index.php");
    printf("YES!!");
  }else{
    printf("Nope");
   
   }
}
}
?>

<!-- create_entry.php -->
<?php

mysql_connect("localhost", "root") or
    die ("Could not connect to database");
mysql_select_db("user") or
    dir ("Could not select database");   
 
if ($submit == "Register")
{
   $query = "insert into user
           (username, password) values ('$username', '$password')";
   mysql_query($query) or
             die (mysql_error());
?>
<h3>You are now registered</h3>
<a href="index.html">Go back to main page</a>
<?php
}
else
{
  include("not.html");
}
?>

請問是否告知問題出在哪裡呢?

另外, 請問若是直接瀏覽test檔案夾裡的index.html的畫會要求你先登入的php開怎麼寫?
謝謝

離線

 

#2 2005-04-07 14:49:36

tonynet
Member
註冊日期: 2005-04-01
文章數: 31

Re: 請問關於一個簡單的測試登入註冊系統

pinwith 寫道:

但是總會有問題

是什麼問題,error message???

pinwith 寫道:

<!-- login.php -->
<?
if ($submit = "Login")
{

if ($submit = "Login") <----------------------  if ($submit == "Login")

pinwith 寫道:

另外, 請問若是直接瀏覽test檔案夾裡的index.html的畫會要求你先登入的php開怎麼寫?
謝謝

試下用 .htaccess 對access control

離線

 

#3 2005-04-07 15:09:38

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

Re: 請問關於一個簡單的測試登入註冊系統

這位大大感謝你指點迷津@@
我已將
if ($submit = "Login")  改為 if ($submit == "Login")

不過當我不管用資料庫裡有的username和password或是不存在的username和password來登入,
都會停留在login.php並且整個頁面空白,請問是什麼原因所造成的?

<?
if ($submit == "Login")
{

  mysql_connect("localhost", "root") or               
    die ("Could not connect to database");

  mysql_select_db("user") or
    dir ("Could not select database");   

$result=mysql_query("select * from user where username='$username'")
             or die ("cant do it");

while ($row=mysql_fetch_array($result)) {
if ($row["password"]==$password )
  {
    header("Location: test/index.php");
  }else{
    printf("Nope");
   
   }
}
}
?>

另外, 在register那註冊以後, 都會直接跑去not.html
並不會顯示告知完成註冊..請問是否知道為什麼?

離線

 

#4 2005-04-07 16:07:41

tonynet
Member
註冊日期: 2005-04-01
文章數: 31

Re: 請問關於一個簡單的測試登入註冊系統

我的做法,請參考


login.php
<?php
//沒有輸入
if ( (empty($_POST[username])) || (empty($_POST[userpasswd])) ){
//chose you want
//echo 'error';
//header("Location:xxxx"); //redirect other
//include("xxxx.html"); 
//exit();

else:
//有輸入
//把所有可能會 SQL injection 當然登記時不給user chose special 字元
$_POST[username]=htmlentities($_POST[username], ENT_QUOTES);
$_POST[userpasswd]=htmlentities($_POST[userpasswd], ENT_QUOTES);

$mysqlhost='*****';
$mysqlpasswd='*****';
$mysqluser='*****';
$mysqldbf='*****';

$link_ID = mysql_connect($mysqlhost,$mysqluser,$mysqlpasswd);
mysql_select_db($mysqldbf);

$str="SELECT * FROM your_db where username = '$_POST[username]' and userpasswd = '$_POST[userpasswd]' ;";
$result = mysql_query($str,$link_ID);
$record=mysql_fetch_array($result);

if (empty($record)): //無record
//chose you want
//echo 'error';
//header("Location:xxxx"); //redirect other
//include("xxxx.html"); 
//exit();


else: //有record

//再驗一次passwd  因為太多SQL injection
if ( ($_POST[userpasswd]<>$record[userpasswd]) || ($_POST[username]<>$record[username]) ):
//chose you want
//echo 'error';
//header("Location:xxxx"); //redirect other
//include("xxxx.html"); 
//exit();


else:

//login 成功
//chose you want
//echo 'ok';
//header("Location:xxxx"); //redirect other 當然要用cookie or session
//include("xxxx.html"); 
//exit();

endif;

endif;


}
?>

離線

 

#5 2005-04-11 13:12:25

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

Re: 請問關於一個簡單的測試登入註冊系統

謝謝你的指導@@"
不過當我測試您的方法的時候,
它顯示有1行出現error:

else:   <- 這一行..
//有輸入
//把所有可能會 SQL injection 當然登記時不給user chose special 字元
$_POST[username]=htmlentities($_POST[username], ENT_QUOTES);
$_POST[userpasswd]=htmlentities($_POST[userpasswd], ENT_QUOTES);

請問問題在哪裡呢?

離線

 

#6 2005-04-11 13:24:03

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

Re: 請問關於一個簡單的測試登入註冊系統

pinwith 寫道:

謝謝你的指導@@"
不過當我測試您的方法的時候,
它顯示有1行出現error:

else:   <- 這一行..
//有輸入
//把所有可能會 SQL injection 當然登記時不給user chose special 字元
$_POST[username]=htmlentities($_POST[username], ENT_QUOTES);
$_POST[userpasswd]=htmlentities($_POST[userpasswd], ENT_QUOTES);

請問問題在哪裡呢?

我試著把一開始的
if ( (empty($_POST[username])) || (empty($_POST[password])) ){

改為
if ( (empty($_POST[username])) || (empty($_POST[password])) ):

然後把最後 ?> 之前的 } 刪掉,
不過卻一跑出一個error, 在 ?> 這一行上面@@"
結果還是不知道問題在哪..

離線

 

#7 2005-04-11 16:05:28

tonynet
Member
註冊日期: 2005-04-01
文章數: 31

Re: 請問關於一個簡單的測試登入註冊系統

//沒有輸入
if ((empty($_POST[username])) || (empty($_POST[userpasswd]))) {

    //chose you want
    //echo 'error';
    //header("Location:xxxx"); //redirect other
    //include("xxxx.html");
    //exit();


} else {

    //有輸入
    //把所有可能會 SQL injection 當然登記時不給user chose special 字元
    $_POST[username]=htmlentities($_POST[username], ENT_QUOTES);
    $_POST[userpasswd]=htmlentities($_POST[userpasswd], ENT_QUOTES);

    $mysqlhost='*****';
    $mysqlpasswd='*****';
    $mysqluser='*****';
    $mysqldbf='*****';

    $link_ID = mysql_connect($mysqlhost,$mysqluser,$mysqlpasswd);
    mysql_select_db($mysqldbf);

    $str="SELECT * FROM your_db where username = '$_POST[username]' and userpasswd = '$_POST[userpasswd]' ;";
    $result = mysql_query($str,$link_ID);
    $record=mysql_fetch_array($result);
   
   
    if (empty($record)) {//無record

        //chose you want
        //echo 'error';
        //header("Location:xxxx"); //redirect other
        //include("xxxx.html");
        //exit();

    } else {//有record
   
        if (($_POST[userpasswd]<>$record[userpasswd]) || ($_POST[username]<>$record[username])) {//再驗一次passwd  因為太多SQL injection

            chose you want
            //echo 'error';
            //header("Location:xxxx"); //redirect other
            //include("xxxx.html");
            //exit();

        } else {

            //login 成功
            //chose you want
            //echo 'ok';
            //header("Location:xxxx"); //redirect other 當然要用cookie or session
            //include("xxxx.html");
            //exit();

        }

    }

}

離線

 

#8 2005-04-13 07:18:06

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

Re: 請問關於一個簡單的測試登入註冊系統

喔喔喔喔喔終於成功終於懂了~
真的是非常感謝你..不好意思一直打擾..
雖然執行的時候有error出現, 說mysql_fetch_array:這個程式不是有效的*(#%(

tonynet 寫道:

$str="SELECT * FROM your_db where username = '$_POST[username]' and userpasswd = '$_POST[userpasswd]' ;";
    $result = mysql_query($str,$link_ID);
    $record=mysql_fetch_array($result);

我一直腸是不同的寫法還是會出現錯誤,
後來我終於發現問題在哪了..................
$str="SELECT * FROM your_db
原來其實是要填表單..我卻填成資料庫...難怪一直出現錯誤讀取不了資料@@

還是仔細看了前面mysql_query這個function才發現的...
小小的login page終於弄成功了...(泣)

目前繼續弄regist的頁面, 感謝這位大大喔^^
PS. 有問題的時候再繼續來打擾您= ="

離線

 

#9 2005-04-13 09:15:55

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

Re: 請問關於一個簡單的測試登入註冊系統

不好意思我又回來了...
我的regiser雖然很簡單,不會偵測是否有htmlcode等等的,但總算是可以順利的增加username///password到資料庫了,

不過現在當我想要進階一點想要讓他能夠執行logoff,以及若是直接連結到test/index.php 會要求您先login,
問題就來了...
login和register沒問題,問題在於每當直接開啟test/index.php的時候,
便會直接出現u r now log-in test test (這是設定當判定有login的時候會顯示的)

我用的方法是設定cookie,因此在login.php裡面,當檢查username和password正確以後,

  session_start();
  session_unset();
  session_destroy();
  @ session_register("password");
  $HTTP_SESSION_VARS["password"] = $password;
  @ session_register("username");
  $HTTP_SESSION_VARS["username"] = $username;
  setcookie("rememberCookieUname",$username,(time()+604800));
  setcookie("rememberCookiePassword",md5($password),(time()+604800));
               
header("Location:test/index.php");

然後在logoff.php寫了:

  <?PHP
  session_start();
  setcookie("rememberCookieUname","session expired",(time()+604800));
  setcookie("rememberCookiePassword","X@X",(time()+604800));
  session_unset();
  session_destroy();
  header("Location: index.html");
  ?>

另外新增了一個check.php (這是include在test/index.php裡面用來判定是否有login):

<?PHP
    $rememberCookieUname = @$HTTP_COOKIE_VARS["rememberCookieUname"];
    $rememberCookiePassword = @$HTTP_COOKIE_VARS["rememberCookiePassword"];
   
    $mysqlhost='localhost';
    $mysqlpasswd='';
    $mysqluser='root';
    $mysqldbf='user';

    mysql_connect($mysqlhost,$mysqluser,$mysqlpasswd);
    mysql_select_db($mysqldbf);
   
    session_start();
   
    $query = "Select * from users where username='".@$HTTP_SESSION_VARS[username]."' And password = '".@$HTTP_SESSION_VARS[password]."'";
    $result = mysql_query($query);
    if ($row = mysql_fetch_array($result)){
        echo 'u r now log-in';
    }else{
        if ($rememberCookiePassword != "" && $rememberCookieUname != ""){
            $query = "Select * from users where username='".$rememberCookieUname."'";
            $result = mysql_query($query);
           
            if ($row = mysql_fetch_array($result)){
                if (md5($row["password"]) == $rememberCookiePassword){
                    session_unset();
                    session_destroy();
                    @ session_register("password");
                    $HTTP_SESSION_VARS["password"] = $rememberCookiePassword;
                    @ session_register("username");
                    $HTTP_SESSION_VARS["username"] =  $rememberCookieUname;
                }else{
                    die("Please log-in.");
                }
            }else{
                die("Please log-in.");
            }
        }else{
            die("Please log-in.");
        }
    }
   
   
?>

請問cookie這樣的用法正確嘛?
若是不用cookie的話是否還有其他比較簡便的方法可以完成判定是否有login的程序?

感謝賜教@@"

離線

 

#10 2005-04-13 19:27:16

tonynet
Member
註冊日期: 2005-04-01
文章數: 31

Re: 請問關於一個簡單的測試登入註冊系統

成功login

<?php
session_start();
$_SESSION['successfull_login'] = 'yes';
?>

每頁php 開頭 include this or file
<?php
session_start();
if ($_SESSION['successfull_login']<>'yes') {
unset($_SESSION['successfull_login']);
//error goto login page
//header("Location:xxxx"); //redirect other
exit();
}
?>


logout page
<?php
session_start();
unset($_SESSION['successfull_login']);
?>

離線

 

#11 2005-04-13 23:32:36

pinwith
Member
註冊日期: 2005-04-07
文章數: 17

Re: 請問關於一個簡單的測試登入註冊系統

原來只要這短短的幾句就..................
我沒事果然很愛鑽牛角尖...
一個簡單的管理系統包含登入登出以及註冊總算是完成了...
真的非常感謝您~
終於可以繼續開發下去了 smile

離線

 

#12 2007-05-25 00:19:45

sp69hk
New member
註冊日期: 2007-05-24
文章數: 1

Re: 請問關於一個簡單的測試登入註冊系統

謝謝Tony君!我也攪了很久,原來是欠了_post這個指令...唉, 咁就幾粒鐘~

離線

 

論壇頁尾

Web Hosting
PHP Developer Channel
Powered by PunBB 1.2.9
© Copyright 2002–2005 Rickard Andersson