nethken nethken - 6 months ago 10
PHP Question

Pass variable from another page

I can't pass the variable to another page using session and i don't know what is the error here. Can someone help me? and clues on how to use the session? i want to pass the variable username to another page.

here is the html code. this is my login page where the variable is coming from.

<div class="wrapper">
<form class="logform" action="" method="post">
<p class="title">Admin Login</p>
<input type="user" name="uname" placeholder="Username" autofocus/>
<i class="fa fa-user"></i>
<input type="password" name ="pword" placeholder="Password" />
<i class="fa fa-key"></i>
<a href="#">Forgot your password?</a>
<button type="submit" value="login" name="login">
<i class="spinner"></i>
<span class="state">Log in</span>


$user = $_POST["uname"];
$password = $_POST["pword"];
$name = mysqli_real_escape_string($con, $user);
$password = mysqli_real_escape_string($con, $password);
$select_user="SELECT * FROM accounts WHERE username ='$user' AND password ='$password'";
$run_user = mysqli_query($con, $select_user);
$check_user = mysqli_num_rows($run_user);
if($check_user > 0){
$_SESSION['uname'] = $username;
echo "wrong";

here is the admin page.


<h1>Welcome, <?php echo $username; ?>!</h1>


You're setting the session value on the first page:

$_SESSION['uname'] = $username;

But you never read it on the second page. Read the session value:

$username = $_SESSION['uname'];

Edit: Come to think of it, you're never even setting the $username variable on the first page. Did you mean to use $user instead? Or $name?

You can transfer a value via session exactly as you are, but there has to be a value to transfer in the first place.