Click here to Skip to main content
14,452,666 members

Notice: undefined index: username in C:\wamp64\www\115\index.php on line 19.........I cant access my session variable values

Lawnil asked:

Open original thread
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
Tags: HTML, PHP, MySQL

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100