设置要在profile.php页面上使用的会话变量

时间:2018-06-24 07:49:59

标签: php mysqli session-variables

the problem

my code

当我通过localhost注册时,代码有效,但是当我上载网站时 我修复了大多数错误,但给了我很多错误,但是其中有一个会话变量

这是我的网站,如果您想亲自查看..填写表格,您将看到问题www.cancelliano.com

这是我的注册码 还请检查图片

 <?php


$mysqli = mysqli_connect("myhost","myuser name","my pass","my database name");

>// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
// Set session variables to be used on profile.php page
<?php
$_SESSION['first_name'] = $_POST['first_name'];
$_SESSION['last_name'] = $_POST['last_name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['ty'] = $_POST['ty'];
$_SESSION['date'] = $_POST['date'];
$_SESSION['password'] = $_POST['password'];

>// Escape all $_POST variables to protect against SQL injections
$first_name = $mysqli->escape_string($_POST['first_name']);
$last_name = $mysqli->escape_string($_POST['last_name']);
$email = $mysqli->escape_string($_POST['email']);
$ty = $mysqli->escape_string($_POST['ty']);
$date = $mysqli->escape_string($_POST['date']);
$password = $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
$hash = $mysqli->escape_string( md5( rand(0,1000) ) );

>// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error);

>// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {

    $_SESSION['message'] = 'User with this email already exists!';
    header("location: error.php");

}
else { // Email doesn't already exist in a database, proceed...

    // active is 0 by DEFAULT (no need to include it here)
    $sql = "INSERT INTO users ( first_name, last_name, email,ty,date, password, hash) " 
            . "VALUES ('$first_name','$last_name','$email','$ty','$date','$password', '$hash')";

    // Add user to the database
    if ( $mysqli->query($sql) ){

        $_SESSION['active'] = 0; //0 until user activates their account with verify.php
        $_SESSION['logged_in'] = true; // So we know the user has logged in
        $_SESSION['message'] =



        header("location: profile.php"); 

    }

    else {
        $_SESSION['message'] = 'Registration failed!';

    }

}

1 个答案:

答案 0 :(得分:0)

尝试在所有内容中包括以下代码,但您的代码看不到会话开始。

session_start();