- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这个主题上进行了大量搜索,并获得了很多不错的(但不同的)结果。有些结果并不完全相关,并且最终似乎似乎是一个偏爱的问题,但是我对遵循良好的设计原则还是不感兴趣感兴趣。
如果这个问题含糊不清,请随时删除它,但是您可以建议在哪里发布它吗?
另外,这是,只是的一个示例。我通常会在这里做很多事情,但是为了简单起见,我是这样做的。
代码很长,但是您应该可以直接将其复制并粘贴到一个新的PHP文件中,然后在您的环境中运行它。无需任何设置。
具体问题
<?php
error_reporting ( E_ALL | E_STRICT );
class MemberLoginException extends Exception
{
public function __construct ( $message = null, $code = 0, Exception $previous = null )
{
parent::__construct ( $message, $code, $previous );
}
}
class AccountsInsertException extends Exception
{
public function __construct ( $message = null, $code = 0, Exception $previous = null )
{
parent::__construct ( $message, $code, $previous );
}
}
class AccountsManager
{
protected $_accounts = array ();
protected $_lcUsernames = array (); # all usernames in lowercase for checking if username is taken
public function __construct ( array $accounts = null )
{
$this->setAllAccounts ( $accounts );
}
public function __destruct ()
{
unset ( $this->_accounts, $this->_lcUsernames );
}
public function __toString ()
{
$return = '';
if ( count ( $this->_accounts ) > 0 ) :
$return = '<table>';
$return .= '<tr><th>Username</th><th>Password</th></tr>';
foreach ( $this->_accounts as $account ) :
$return .=
'<tr>
<td>'. htmlentities ( $account['username'], ENT_QUOTES, 'UTF-8' ) . '</td>
<td>'. htmlentities ( $account['password'], ENT_QUOTES, 'UTF-8' ) . '</td>
</tr>';
endforeach;
$return .= '</table>';
return $return;
endif;
}
public function Clear ()
{
$this->_accounts = array ();
$this->_lcUsernames = array ();
}
public function Authenticate ( Member $member )
{
$username = strtolower ( $member->getUsername () );
if ( count ( $this->_accounts ) ) :
foreach ( $this->_accounts as $account ) :
if ( strtolower ( $account['username'] ) == $username )
return ( bool ) ( $account['password'] == $member->getPassword () );
endforeach;
else :
return false;
endif;
}
public function getAllAccounts ()
{
return $this->_accounts;
}
public function setAllAccounts ( array $newValue = null )
{
if ( is_null ( $newValue ) )
$this->_accounts = array ();
else
$this->_accounts = $newValue;
$this->_lcUsernames = array ();
foreach ( $this->_accounts as $account )
$this->_lcUsernames[] = strtolower ( $account['username'] );
return $this;
}
public function hasAccount ( $username )
{
return in_array ( strtolower ( $username ), $this->_lcUsernames, false );
}
public function AddAccount ( $username, $password )
{
/*
Faster to be redundant by storing a lowercase copy of the username for comparison
if ( array_key_exists ( strtolower ( $username ), array_change_key_case ( $this->_accounts ) ) )
throw new AccountsInsertException ( 'Unable to create account; account already exists.' );
*/
if ( $this->hasAccount ( $username ) )
throw new AccountsInsertException ( 'Unable to create account; account already exists.' );
$this->_accounts[] = array (
'username' => $username,
'password' => $password,
);
$this->_lcUsernames[] = strtolower ( $username );
return $this;
}
public function RemoveAccount ( $username )
{
if ( $this->hasAccount ( $username ) ) :
unset ( $this->_accounts[$username] );
unset ( $this->_lcUsernames [ strtolower ( $username ) ] );
endif;
return $this;
}
public function __Debug ()
{
echo "\r<pre>\r";
print_r ( $this->_accounts );
echo "\r</pre>\r\r\r<pre>\r";
print_r ( $this->_lcUsernames );
echo "\r</pre>\r\r";
}
}
class Member
{
protected $_username = '';
protected $_password = '';
public function __construct ( $username, $password )
{
$this->setUsername ( $username );
$this->setPassword ( $password );
}
public function getUsername ()
{
return $this->_username;
}
public function setUsername ( $newValue )
{
$this->_username = ( string ) $newValue;
return $this;
}
public function getPassword ()
{
return $this->_password;
}
public function setPassword ( $newValue )
{
$this->_password = ( string ) $newValue;
return $this;
}
}
# create a new accounts manager which stores all accounts and handles authentication
# the Member class would be responsible for setting session variables, etc. Manager just checks user/pass.
$manager = new AccountsManager ();
?><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
*
{
font-family: "Segoe UI", "Trebuchet MS", Tahoma, Arial, Helvetica, sans-serif;
}
body
{
margin: 4em 6em;
line-height: 1.6em;
font-size: smaller;
}
header
{
border-bottom: 2px solid #efefef;
margin-bottom: 3em;
padding-bottom: 1em;
}
h1, h2, h3, h4, h5, h6
{
font-weight: normal;
letter-spacing: 1px;
color: royalblue;
}
h5, h6
{
font-weight: bold;
}
header h1 sub, header h1 sup
{
font-size: small;
color: #FF4400;
letter-spacing: 2px;
}
section
{
border-bottom: 1px dotted #ccc;
padding-bottom: 2em;
margin-bottom: 3em;
}
table
{
border: 1px solid #eee;
padding: 1em;
border-right-width: 2px;
border-bottom-width: 2px;
}
th
{
text-align: left;
font-variant: small-caps;
border-bottom: 1px dotted #ccc;
padding-bottom: .75em;
margin-bottom: .75em;
letter-spacing: 1px;
color: #FF4400;
}
td:hover
{
background-color: skyblue;
}
td
{
margin: 0;
display: table-cell;
padding: .5em;
}
pre
{
font-family: "Droid Sans Mono", Consolas, "Courier New", Courier, monospaced;
border: 1px solid #E4E4E4;
padding: 1em;
line-height: 1em;
}
.error
{
color: red;
border: 1px dotted #ccc;
}
.success
{
color: forestgreen;
border: 1px dotted #e0e0e0;
}
.error, .success
{
padding: .75em;
background-color: #FFFFCC;
border: 1px solid #E4E4E4;
}
</style>
<title>Sample Login System - Test Exceptions</title>
</head>
<body>
<header>
<h1>Simple Login System <sup>demonstrating exceptions…</sup></h1>
</header>
<section>
<h2>No database required</h2>
<p>To avoid time setting up your environment, this test simply uses a class that stores an array of accounts.
Obviously, this isn't persistent (at this time) and it doesn't actually save anything anywhere except in the
array during the script's lifetime. Upon the next request, the previous accounts will be erased.</p>
</section>
<section>
<h2>Creating accounts...</h2>
<?php
$createList =
array (
array (
'username' => 'Daniel Elkins',
'password' => 'delkins[not-pass-for-anything]',
),
array (
'username' => 'Jennifer Lynn',
'password' => 'lilJenn',
),
array (
'username'=> 'Charlie Dog',
'password'=> 'grrrrr',
),
);
if ( $manager->setAllAccounts ( $createList ) instanceof AccountsManager ) : ?>
<p><strong>Accounts were created successfully!</strong> They should be listed in
a table below.</p>
<?php
else :
?>
<p class="error">There was an error creating your accounts...</p>
<?php
endif;
?>
</section>
<section>
<h2>List of accounts</h2>
<?php echo $manager; ?>
</section>
<section>
<h2>Trying to create one that already exists...</h2>
<?php
try
{
$manager->AddAccount ( 'Daniel Elkins', 'delkins[not-pass-for-anything]'); ?>
<p class="success">Account created successfully!</p>
<?php
}
catch ( AccountsInsertException $exception )
{
?>
<p class="error"><?= $exception->getMessage (); ?></p>
<?php
}
?>
</section>
<section>
<h2>Showing accounts again</h2>
<?php echo $manager; ?>
</section>
<section>
<h2>Valid login test</h2>
<p>Logging in user `Daniel Elkins`…</p>
<?php
if ( $manager->Authenticate ( new Member ( 'Daniel Elkins', 'delkins[not-pass-for-anything]' ) ) ) : ?>
<p class="success">Authentication successful!</p>
<?php
else :
?>
<p class="error">Unable to login; invalid username or password!</p>
<?php
endif;
?>
</section>
<section>
<h2><strong>Invalid</strong> login test</h2>
<p>Logging in user `Doesnt_Exist`…</p>
<?php
if ( $manager->Authenticate ( new Member ( 'Doesnt_Exist', '1234' ) ) ) : ?>
<p class="success">Authentication successful!</p>
<?php
else :
?>
<p class="error">Unable to login; invalid username or password!</p>
<?php
endif;
?>
</section>
<section>
<h2>Debug information</h2>
<?php $manager->__Debug (); ?>
</section>
</body>
</html>
最佳答案
Is this the correct way to use exceptions and handle them on the caller side?
Should I even be using exceptions for this?
Are my skeleton custom exceptions correct?
AccountInsertException
中创建的帐户的名称。可能不是必需的,但是如果您发现自己可能会有用,那就只是要考虑的事情。
关于php - PHP-是否在类中正确使用了异常进行错误处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10169256/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!