我的脚本中有一个未定义变量的错误



我正在使用PHP创建登录和注册系统,这是我在为user_id 定义变量时在注册页面上遇到的错误

致命错误:未捕获错误:调用C:\examplep\htdocs\login\signup.php中未定义的函数random_num((:17堆栈跟踪:#0{main}在第17行的C:\examplep\tdocs\login\ signup.php中抛出

这是我的注册代码.php

<?php
//start of session
session_start();

//include files
include("connection.php");
include("functions.php");
//need to check if the user has clicked on the post button 
if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
// It means that someone has already signed up so we need to username and password
$username = $_POST['user_name'];
$password = $_POST['password'];
//we need to check if username and password are not empty also we can use different conditions for the username and password before savign them to database 
if(!empty($username)&& !empty($password) && !is_numeric($username)){
//If everthing is correct then we will add to the database 
$user_id= random_num(20);
$query = "insert into users (user_id,user_name,password) values ($user_id,$user_name,$password)";
mysqli_query($query);

//once everything is one lets redirect the user to the login page 
header(Location:login.php);
die;
} else {
echo "Please enter some valid information!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sign Up</title>
<style type="text/css">/*truncated*/</style> 
</head>
<body>
<div id="box">
<form method="post">
<div style="font-size: 20px;margin: 10px;color: white;">Sign Up</div>
<input id="text" type="text" name="user_name"><br><br>
<input id="text" type="password" name="password"><br><br>
<input id="button" type="submit" value="Signup"><br><br>
<a href="login.php">Click to Login</a>
</form>
</div>
</body>
</html>

我还附上了我制作的函数文件的代码。我正在学习PHP,因此需要确定的问题所在

<?php
// so what we are doing is chcecking if the session value exists then stay on the index page otherwise redirect to the login.php
//this fucntion will check if the user is logged in 
function check_login($con){
// first we will check the session value 
if(isset($_SESSION['user_id'])){
$id = $_SESSION['user_id'];
//LETS CHECK IN THE DATABASE
$query = "SELECT * FROM users WHERE user_id = '$id' LIMIT 1";

//read from database
$result = mysql_query($con, $query);

//now check if the result is positive and the number of rows are greater than zero
if ($result && mysqli_num_rows($result)>0) {
//assoc is the associtive array
$user_data = mysqli_fetch_assoc($result);
return $user_data;
}
}
//redirect to login
header("Location: login.php");
}
//function for the $user_id
function random_num($length){
$text = "";
// to make sure the lenght is never less than 5 
if($length<5){
$length=5;  
}

//assign a variable between 4, the number we have whihc is 20
$len = ran(4,$length);
for( $i = 0; $i < $len; $i++){ 
# code...
$text.=rand(0,9);
}

return $text;
}

因此,它向我显示错误在第17行的登录文件中,我在其中定义了user_id=random_num(20);

此外,如果你需要了解更多关于我的项目的信息,请附上connection.php以及的代码

<?php
//connection to database 
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "");
//checking if the connection is made succesful or not 
if(!$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)){
die("failed to connect");
}

这里有几个问题。第一个是格式化,注意间距/缩进等。我不得不把它粘贴出来并重新格式化,以便轻松阅读正在发生的事情。你未来的自己(和其他开发者(会感谢你的!

一旦稍微清理一下,就更容易看到发生了什么

将用户数据直接插入数据库

这可能是因为你正在学习(我们都去过!(,但如果不首先验证和清理用户提供的数据,就永远不要将其放入数据库,以免受到SQL注入攻击。您需要尽快查看准备好的报表。

同样,在你的第一块代码中,header(Location:login.php);应该是header('Location: login.php');(也可以尝试使用完全实现的URL,而不是相对路径,因为这些路径依赖于当前文件的位置

还有其他一些小问题,但在主要问题之前的最后一个问题是,random_num()函数中有一个拼写错误,$len = ran(4,$length);ran()不是函数。如果它正在运行,您会得到另一个未定义的函数错误。(附带说明,考虑使用mt_rand()而不是rand()(

此外,为什么要生成随机数作为用户ID?在某个时刻将会发生碰撞。您应该在数据库中使用AUTO_INCREMENT整数字段作为用户ID;大数字";出于某种原因,您可以始终将它们显示为不同的基础,加密它们,等等。但您应该从用户ID 1开始,让自动增量处理其余部分。


哇!所有这些都解决了,看起来你的functions.php文件没有被包括在内(否则,你会得到ran()是一个未定义的函数错误

当包括";核心";像这样的文件,应该使用require_once而不是include。不同的是,如果找不到文件,require会出错。你可能把它放在错误的位置,或者它的名称错误的function.php/functions.php等等。使用require_once的另一个好处是,如果你再添加第二个文件和include函数,你会得到";已定义的";函数错误,如果已经找到文件,则使用require_once将避免第二次加载文件。

我不会为自动加载而打扰你(但你可能想调查一下(。但是,确保您已经修复了header()ran()错误,并确保您有正确的文件名位置,使用require_once应该可以让您启动并运行。


此外,我再次强调,在没有验证/消毒的情况下,不要将用户提供的输入直接放入数据库!

定义random_num((函数或使用rand((或random_int((。

最新更新