我正在尝试设计一个系统,如果登录正确,我将使用会话将用户名从 login.php 发送到 upload.php 。 每次我尝试时,都会显示一个错误: 注意:未定义的索引:第70行的C:\ xampp \ htdocs \ interface \ upload.php中的用户名 请注意,我已经检查了 upload.php 的第70行,我认为没有任何错误。我的文件上传还可以,但是用户名没有通过会话传输 如果您请解释我的代码出了什么问题,那将非常有帮助。 TIA。
db.sql
create database profiles;
use profiles;
create table users
(
id int(10) primary key,
pass varchar(50)
);
create table imageP
(
id int(10),
location varchar(100),
foreign key (id) references users(id)
);
insert into users values(1,'1');
login.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" name="login">
<input type="number" name="username" placeholder="Enter a Username"/>
<input type="password" name="password" placeholder="***"/>
<input type="submit" name="login" value="Login"/>
</form>
</body>
</html>
<?php
if(isset($_POST['login'])){
$errmsg_arr = array();
$errflag = false;
// configuration
$dbhost = "localhost";
$dbname = "profiles";
$dbuser = "root";
$dbpass = "";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8mb4");
// new data
$user = $_POST['username'];
$password = $_POST['password'];
if($user == '') {
$errmsg_arr[] = 'You must enter your Username';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'You must enter your Password';
$errflag = true;
}
// query
$result = $conn->prepare("SELECT * FROM users WHERE id= :u AND pass= :p");
$result->bindParam(':u', $user);
$result->bindParam(':p', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
$_SESSION['username'] = $user;
header("location: http://localhost/interface/upload.php");
}
else{
$errmsg_arr[] = 'Username and Password are not found';
echo(errmsg_arr);
sleep(3);
header("location: http://localhost/interface/login.php");
$errflag = true;
}
}
?>
upload.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
$username = $_SESSION['username']; // holds url for last page visited.
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo 'hi '.$username;
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
答案 0 :(得分:0)
从页面顶部开始会话,并检查session_start()之前是否没有空格。在任何html代码之前。
答案 1 :(得分:0)
在session_start();
之后,将login.php
放在顶部的DOCTYPE
中。
然后尝试首先在$_POST
upload.php
$_SESSION['username'] = $_POST['username'];
然后,您现在可以将$_SESSION
定义为$username
,
$username = $_SESSION['username'];