Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cant access my variable values of the session from another page.


<?php

session_start();

require_once 'config/db.php';

$errors=array();
$username="";
$email="";


if(isset($_POST['signup-btn'])){
	$username=$_POST['username'];
	$email=$_POST['email'];
	$password=$_POST['password'];
	$passwordConf=$_POST['passwordConf'];


	if(empty($username)){
		$errors['username']="Username required";
	}

	if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
		$errors['email']="Email address is invalid!";
	}



	if(empty($email)){
		$errors['email']="Email required";
	}
	if(empty($password)){
		$errors['password']="Password required";
	}

	if($password !== $passwordConf){
         $errors['password']="The two passwords do not match";
	}

	$emailQuery = "select * from users where email=? limit 1";
	$stmt = $conn->prepare($emailQuery);
	$stmt->bind_param('s',$email);
	$stmt->execute();
	$result=$stmt->get_result();
	$userCount=$result->num_rows;
	$stmt->close();

	if($userCount > 0){
		$errors['email'] = "Email already exists";
	}


	if(count($errors)===0){
       $password = password_hash($password, PASSWORD_DEFAULT);
       $token = bin2hex(random_bytes(50));
       $verified = false;

       $sql =  "insert into users(username,email,verified,token,password)values(?,?,?,?,?)";
       $stmt = $conn->prepare($sql);
	   $stmt->bind_param('ssiss',$username,$email,$verified,$token,$password);
	   if($stmt->execute()){

	   	$user_id = $conn->insert_id;
	   	$SESSION['id']= $user_id;
	   	$SESSION['username']=$username;
	   	$SESSION['email']=$email;
	   	$SESSION['verified']=$verified;

	   	$_SESSION['message']="You are now logged in!";
	   	$_SESSION['alert-class']="alert-success";
	   	header('location:index.php');
	   	exit();
 
    }
	   
	else{
	   	$errors['db_error'] = "Database error: failed to register";
	   }
	
}

 

}



This is my index file below

<?php require_once 'controllers/authController.php'; ?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
	<link rel="stylesheet" href="style.css">
	<title>Home Page</title>
</head>
<body>
	
	<div class="container">
		<div class="row">
			<div class="col-md-4 offset-md-4 form-div login">
				<div class="alert <?php echo $_SESSION['alert-class']; ?>">
					<?php echo $_SESSION['message']; ?>
				</div>
				<h3>Welcome, <?php echo $_SESSION['username']; ?></h3>
				<a href="#" class="logout">logout</a>

				<?php if(!$_SESSION['verified']): ?>
				<div class="alert alert-warning">
					You need to verify your account.
					Sign in to your email account and click on the verification link we just emailed you at
					<?php echo $_SESSION['email']; ?>

          			</div>
          		<?php endif; ?>


          			<?php if($_SESSION['verified']): ?>
          			<button class="btn btn-block btn-lg btn-primary">I am verified!</button>
          			<?php endif; ?>
			</div>
		</div>
	</div>

</body>
</html>


What I have tried:

I tried starting another session in the index page but i get a warning saying two sessions are being run together
Posted
Updated 24-Sep-19 5:11am

1 solution

You're not storing anything in $_SESSION['username'], you are storing it in $SESSION['username']
 
Share this answer
 
Comments
Lawnil 24-Sep-19 11:17am    
Yes thank you! The code now runs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900