求一个php登录的代码

q274256181 2010-04-26 11:44:53
MYSQL数据库
数据库名称:nd
表名:user
字段:name/password
求高人给个登录代码 请写的详细些 我是新手 怕看不懂 谢谢
...全文
113 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yahuD 2010-04-27
  • 打赏
  • 举报
回复
4楼真认真,赞一个,结贴前留名
xiongyafen 2010-04-27
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶
joaryyu 2010-04-27
  • 打赏
  • 举报
回复
4楼的同志都写了,弟兄就不在那个啥 了。。。。。。
jonas 2010-04-27
  • 打赏
  • 举报
回复
php技术联盟 ,新手学习必备参考网站www.diysys.com,
zhouzerong2006 2010-04-27
  • 打赏
  • 举报
回复
这个lz真是个首例
ragnaros87 2010-04-27
  • 打赏
  • 举报
回复
四楼太犀利了,拷走学习学习!
骄傲青蛙 2010-04-27
  • 打赏
  • 举报
回复
no words ...
地雷 2010-04-27
  • 打赏
  • 举报
回复
不会结的吧。。。
HOOLOO 2010-04-27
  • 打赏
  • 举报
回复

还没结贴啊
sciolist 2010-04-26
  • 打赏
  • 举报
回复
这种东西谷歌搜索一下就有,lz怎么不自己动手呢?

1.数据库

--Name the table "dbUsers." It will need 4 fields:
Name Type Addition
id int(10) Primary Key, AUTO_INCREMENT
username varchar(16) Unique
password char(16)
email varchar(25)


数据库基类:
dbConfig.php

<?
// Replace the variable values below
// with your specific database information.
$host = "localhost";
$user = "UserName";
$pass = "Password";
$db = "dbName";

// This part sets up the connection to the
// database (so you don't need to reopen the connection
// again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}

// Then you need to make sure the database you want
// is selected.
mysql_select_db($db);
?>


2.注册功能
register.php

<?php

// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("dbConfig.php");


//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
{
$bInputFlag = false;
foreach ( $_POST as $field )
{
if ($field == "")
{
$bInputFlag = false;
}
else
{
$bInputFlag = true;
}
}
// If we had problems with the input, exit with error
if ($bInputFlag == false)
{
die( "Problem with your registration info. "
."Please go back and try again.");
}

// Fields are clear, add user to database
// Setup query
$q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
."VALUES ('".$_POST["username"]."', "
."PASSWORD('".$_POST["password"]."'), "
."'".$_POST["email"]."')";
// Run query
$r = mysql_query($q);

// Make sure query inserted user successfully
if ( !mysql_insert_id() )
{
die("Error: User not added to database.");
}
else
{
// Redirect to thank you page.
Header("Location: register.php?op=thanks");
}
} // end if


//The thank you page
elseif ( $_GET["op"] == "thanks" )
{
echo "<h2>Thanks for registering!</h2>";
}

//The web form for input ability
else
{
echo "<form action=\"?op=reg\" method=\"POST\">\n";
echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
echo "<input type=\"submit\">\n";
echo "</form>\n";
}
// EOF
?>


3.登录功能
login.php

<?php
session_start();
// dBase file
include "dbConfig.php";

if ($_GET["op"] == "login")
{
if (!$_POST["username"] || !$_POST["password"])
{
die("You need to provide a username and password.");
}

// Create query
$q = "SELECT * FROM `dbUsers` "
."WHERE `username`='".$_POST["username"]."' "
."AND `password`=PASSWORD('".$_POST["password"]."') "
."LIMIT 1";
// Run query
$r = mysql_query($q);

if ( $obj = @mysql_fetch_object($r) )
{
// Login good, create session variables
$_SESSION["valid_id"] = $obj->id;
$_SESSION["valid_user"] = $_POST["username"];
$_SESSION["valid_time"] = time();

// Redirect to member page
Header("Location: members.php");
}
else
{
// Login not successful
die("Sorry, could not log you in. Wrong login information.");
}
}
else
{
//If all went right the Web form appears and users can log in
echo "<form action=\"?op=login\" method=\"POST\">";
echo "Username: <input name=\"username\" size=\"15\"><br />";
echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
echo "<input type=\"submit\" value=\"Login\">";
echo "</form>";
}
?>


好了,完了。

lz 结贴吧
thinkinginAOCP 2010-04-26
  • 打赏
  • 举报
回复
把标识登录的用户信息存放在cookie中或者是session中就行了,
$_COOKIE['name'] $_COOKIE['id']等.
有无登录的用户信息->是否登录.
q274256181 2010-04-26
  • 打赏
  • 举报
回复
。。。。刚注册没多久
一共发布了3个问题 目前还都没解决
床上等您 2010-04-26
  • 打赏
  • 举报
回复
结帖率 0.00%

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧