首页 \ 问答 \ 如何在PHP登录系统中使用Observer模式?(How to use the Observer pattern in PHP login system?)

如何在PHP登录系统中使用Observer模式?(How to use the Observer pattern in PHP login system?)

我是设计模式的新手。 我有一个登录系统,其中包含连接到我的数据库的类和类似的东西。

但现在我想在我的PHP代码中包含一个观察者模式。 但我不知道怎么做。 例如,每当有新用户时我通知的用户。 我知道观察者模式是如何工作的,例如它做了什么。 但我不知道如何将它包含在PHP代码中。 那你如何做一个包含观察者模式的登录系统呢?

例如,这是我的数据库连接类:

private $pdo;

function __construct() {
    $this->pdo = new PDO('mysql:host=localhost;dbname=users', '', '');
}

而heres是我在登录文件中使用的代码:

if(isset($_POST['username']) && isset($_POST['password'])) {
    include_once("classes/database.php");
    $db = new DB();
    $result = $db->query("SELECT username, pass FROM users WHERE username='".$_POST['username']."' AND pass='".$_POST['password']."'");

    if(isset($result[0]['username']) && isset($result[0]['password'])) {
        $_SESSION['username'] = $_POST['username'];
        header("Location: start.php?username=".$_SESSION['username']);
    }

I am new at design patterns. And I have a log-in system with classes to connect to my database and stuff like that.

But now I want to include a observer pattern in my PHP code. But I don't know how do to that. For example a user I notified whenever there is a new user. I know how the observer pattern works, what it does for example. But I don't know how to include it to PHP code. So how do you do a login system that includes observer pattern?

For example here is my connection class to my database:

private $pdo;

function __construct() {
    $this->pdo = new PDO('mysql:host=localhost;dbname=users', '', '');
}

And heres is the code i using in the login file:

if(isset($_POST['username']) && isset($_POST['password'])) {
    include_once("classes/database.php");
    $db = new DB();
    $result = $db->query("SELECT username, pass FROM users WHERE username='".$_POST['username']."' AND pass='".$_POST['password']."'");

    if(isset($result[0]['username']) && isset($result[0]['password'])) {
        $_SESSION['username'] = $_POST['username'];
        header("Location: start.php?username=".$_SESSION['username']);
    }

原文:https://stackoverflow.com/questions/21455944
更新时间:2022-05-13 08:05

最满意答案

无需为此使用全局变量 - 可以使用scan来代替 - 请参阅此处

示例 -

生成一个表 -

q)t:0N!([] time:5?.z.p; sym:5?`3; price:5?100f; size:5?10000)
    time                          sym price    size
    -----------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994
    2007.05.21D04:26:13.021438816 llm 7.347808 496
    2010.10.30D10:15:14.157553088 obp 31.59526 1728
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486
    2005.03.06D21:05:07.403334368 mho 86.17972 2318

有一个简单的累加器的例子 - 注意,如果需要,函数可以访问其他的参数(见下一个例子):

q)update someCol:{[a;x;y;z] (a+1)}\[0;time;price;size] from t
    time                          sym price    size someCol
    -------------------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994 1
    2007.05.21D04:26:13.021438816 llm 7.347808 496  2
    2010.10.30D10:15:14.157553088 obp 31.59526 1728 3
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486 4
    2005.03.06D21:05:07.403334368 mho 86.17972 2318 5

假设你想得到cumilative尺寸:

q)update cuSize:{[a;x;y;z] (a+z)}\[0;time;price;size] from t
    time                          sym price    size cuSize
    ------------------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994 3994
    2007.05.21D04:26:13.021438816 llm 7.347808 496  4490
    2010.10.30D10:15:14.157553088 obp 31.59526 1728 6218
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486 11704
    2005.03.06D21:05:07.403334368 mho 86.17972 2318 14022

如果你想让一个以上的var通过扫描,可以通过给它一个更复杂的结构在第一个var中包含更多的值:

q)update cuPriceAndSize:{[a;x;y;z] (a[0]+y;a[1]+z)}\[0 0;time;price;size] from t
    time                          sym price    size cuPriceAndSize
    --------------------------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994 29.07093 3994
    2007.05.21D04:26:13.021438816 llm 7.347808 496  36.41874 4490
    2010.10.30D10:15:14.157553088 obp 31.59526 1728 68.014   6218
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486 102.1188 11704
    2005.03.06D21:05:07.403334368 mho 86.17972 2318 188.2986 14022

No need to use global vars for this - can use scan instead - see here.

Example --

Generate a table -

q)t:0N!([] time:5?.z.p; sym:5?`3; price:5?100f; size:5?10000)
    time                          sym price    size
    -----------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994
    2007.05.21D04:26:13.021438816 llm 7.347808 496
    2010.10.30D10:15:14.157553088 obp 31.59526 1728
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486
    2005.03.06D21:05:07.403334368 mho 86.17972 2318

Example with a simple accumilator - note, the function has access to the other args if needed (see next example):

q)update someCol:{[a;x;y;z] (a+1)}\[0;time;price;size] from t
    time                          sym price    size someCol
    -------------------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994 1
    2007.05.21D04:26:13.021438816 llm 7.347808 496  2
    2010.10.30D10:15:14.157553088 obp 31.59526 1728 3
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486 4
    2005.03.06D21:05:07.403334368 mho 86.17972 2318 5

Say you wanted to get cumilative size:

q)update cuSize:{[a;x;y;z] (a+z)}\[0;time;price;size] from t
    time                          sym price    size cuSize
    ------------------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994 3994
    2007.05.21D04:26:13.021438816 llm 7.347808 496  4490
    2010.10.30D10:15:14.157553088 obp 31.59526 1728 6218
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486 11704
    2005.03.06D21:05:07.403334368 mho 86.17972 2318 14022

If you wanted more than one var passed through the scan, can pack more values into the first var, by giving it a more complex structure:

q)update cuPriceAndSize:{[a;x;y;z] (a[0]+y;a[1]+z)}\[0 0;time;price;size] from t
    time                          sym price    size cuPriceAndSize
    --------------------------------------------------------------
    2002.04.04D18:06:07.889113280 cmj 29.07093 3994 29.07093 3994
    2007.05.21D04:26:13.021438816 llm 7.347808 496  36.41874 4490
    2010.10.30D10:15:14.157553088 obp 31.59526 1728 68.014   6218
    2005.11.01D21:15:54.022395584 dhc 34.10485 5486 102.1188 11704
    2005.03.06D21:05:07.403334368 mho 86.17972 2318 188.2986 14022

相关问答

更多

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)