(PHP的帮助)如果文件中存在具体的编号,请这样做
这是一个PHP相关的问题。(PHP的帮助)如果文件中存在具体的编号,请这样做
我已经搜索了广泛的解决方案,但没有找到适合我的东西。如果有人能帮助我,将非常感激。
我的想法是创建一个登录页面,在该登录页面中,如果向用户发送/发送了“特定”预定注册号码,则登录页面和号码存在于文件中,执行/登录权限允许。
例如,如果用户的注册号码是12345而另一个是123,想先查找用户的号码是否存在,则执行某些操作()。
因此,如果“123”已经存在,那么搜索“12345”将不会相同,因为它将与“234”相同,因此它必须找到特定的一个或多个数字。
如果有所作为,数字之前和/或之后就会有一个“空格”。
这将在现有文件中。
代码:
<?php
$filename = 'datafile.txt';
// Data file may contain the following number(s) or any other
// 123 (no)
// 1234 (match)
// 12345 (no)
// 123456 (no)
// exact match needed to proceed to page fopen($filename);
$searchfor = '1234';
// this needs to be a variable
// I don't know what the number will be used.
$file = file_get_contents($filename);
if (preg_match($file, $searchfor)) {
echo "Match found. Proceed to the page.";
} else {
echo "Match not found. Try again.";
}
fclose($filename);
?>
谢谢。任何帮助表示赞赏,欢呼声。
弗雷德
在一个平面文件存储这些可能不是我会做这样的方式,但如果我是用一个平面文件,下面的例子应该只是罚款:
$nums = "12345 34 45 12345 23"; # use file_get_contents to load from file
$uNum = 34;
if (preg_match("/[^0-9]{$uNum}[^0-9]/", $nums)) {
echo "Access";
} else {
echo "Denied";
}
至于填充$nums
字符串,您可以使用file_get_contents()
来拉入包含所有数字的文件内容。
/ Denotes start of pattern [^0-9] Space in pattern cannot be occupied by any number {$uNum} User number - 34 in the example above [^0-9] Space in pattern cannot be occupied by any number / Denotes end of pattern
从本质上讲,我们的号码不会,如果它有它的两侧一些,而不是一个空间,或者什么都没有找到。
感谢Johanhan,我已经与$数字填充工作。现在,我所要做的就是找出如何为它创建一个表单(在同一个脚本中),并使用输入的人的代码,以反映$ uNumber –
@FredFletcher将'$ uNum'设置为'$ _POST ['uNum']'你就全都设置好了。当然,你需要'
'。 – Sampson美丽!谢谢乔纳森,你一直在帮助!我非常感激,欢呼!我做了一个echo'“
“ - 时间让我得到一些冷眼。再次感谢:) –
你的问题对我来说不是很清楚,但它听起来像正则表达式可能是提供这种验证的最佳方式。 –
你的问题很不清楚。请换句话说,我们可以帮助你。 –
也许在该文件中添加注册号与分隔符ex:|||,然后每当你想检查用户是否存在,你打开文件,并使用explode(“|||”,$ file);你会得到一个包含这些注册数字的数组,然后尝试使用in_array()(如果它存在,它将返回true)。 – HamZa