如何在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
最满意答案
无需为此使用全局变量 - 可以使用
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
相关问答
更多-
select distinct Type by Symbol,Date,Time,Sequence from table 或使用xgroup select distinct Type by Symbol,Date,Time,Sequence from table or use xgroup
-
您可以使用基于每列类型的功能应用来执行此操作,如下所示: q)t:([] a:9?`3;b:9#0N;c:9#0Nf;d:9#0Ni) q)@[t;where (type each flip t) within 5 9h;0^] a b c d --------- kdj 0 0 0 eeg 0 0 0 nce 0 0 0 jog 0 0 0 .. 这适用于将0 ^仅应用于类型介于5和9之间的列(即short,int,long,real和float) 希望这可以帮助 乔纳森 You can do th ...
-
kdb数组映射 - 如何将函数应用于数组的每个元素?(kdb array map - how to apply a function to every element of an array?)[2023-03-21]
见each q)gobble each .z.d + til 5 "16782.csv" "16783.csv" "16784.csv" "16785.csv" "16786.csv" 正方形操作*可以在原子和列表上操作 q)1 2 3 4 * 1 2 3 4 1 4 9 16 See each q)gobble each .z.d + til 5 "16782.csv" "16783.csv" "16784.csv" "16785.csv" ... -
[KDB + / Q]:按顺序应用数据列表(管道)([KDB+/Q]: Apply list of functions over data sequentially (pipe))[2022-05-09]
在左边使用lambda以及over副词(递归的形式)同样,使用apply()的形式将函数应用于表和列: {.[y;(z;x)]}/[t;(xasc;xkey);`sym] sym| val ---| --- a | 1 b | 2 c | 3 Use a lambda on the left as well as the over adverb (form of recursion) Also the dot (.) form of apply is used to app ... -
kdb + / q:将更新变量的迭代过程应用于列(kdb+/q: Apply iterative procedure with updated variable to a column)[2022-08-12]
无需为此使用全局变量 - 可以使用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.0 ... -
在KDB + / q中构建列表(Building a list in KDB+ / q)[2022-03-10]
使用每个来实现这一目标 q) til each 2 3 0 1 0 1 2 Use each to achieve this q) til each 2 3 0 1 0 1 2 -
鉴于问题自最初发布和我之前的回答以来发生了很大变化,这里有一个更新的解决方案: q)delete ind from update index:sum@'ind from (update ind:@\[()!();sym;:;shares*price] from data) where i>=max(first;i)fby sym dt sym price shares index ------------------------------------ ...
-
如果你想使用like -right /: :: q)select from t where any name like/:searchNames name c1 --------- "jim" 0 "john" 1 "john" 2 在这种情况下,您可以简单地使用in因为您没有使用任何通配符: q)select from t where name in searchNames name c1 --------- "jim" 0 "john" 1 "john" 2 If you wish to ...
-
不完全确定你想要什么,但以下使用每个sym的最新price来计算总和rp : q)update rp:sum each @\[()!();sym;:;price] from data dt sym price shares rp ----------------------------------------------------- 2017.01.05D19:45:00.238248239 AAPL 101.2 500 101.2 2017. ...
-
t: ([] time: `time$(); chr: `char$()) t, ([] time: enlist 09:30:00.001; chr: enlist"abcd") 嵌套类型实际上得到了很好的支持,我已经有了表条目,其中列条目本身就是表格。 t: ([] time: `time$(); chr: `char$()) t, ([] time: enlist 09:30:00.001; chr: enlist"abcd") Nested types are in fact well su ...