0

Dea Friends,
it is superb serveice you are doing....lets please talk about SESSION in PHP.My code is below.

<!DOCTYPE html>
<?php session_start(); ?>

<html>

<head>
  <title>Login page</title>
</head>
     <h1 style="width: 20%" >Login Page</h1>
       <fieldset style="width: 30%"> <legend style="background: #59CC57" >Login</legend>
  <body style="background: #B6ECEB">
  <form  method="post" >
  Username:<input type="text" name="username" placeholder="username" />  <br></br>
  Password:<input type="password" name="password" placeholder="********" /><br></br>
  <input type="submit" name="login" value="login" /> <br></br>

  </form>

<?php
   include"conn.php";
   $errormsg="";

if(isset($_POST['login'])){
//$uname= $_POST['username'];
//$pword= $_POST['password'];
$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);
                    $sql = $conn->prepare("SELECT * FROM login1 WHERE L1 = ? AND L2 = ?");
                    $sql->bind_param('ss', $uname, $pword);


                        $sql->execute();
                        $sql->store_result();
                       //$result=$conn->query($sql);

                       if($sql->num_rows>0){
                                   //echo("welcome");
                                   $_SESSION['User']=$uname['username'];
                                   $_SESSION['Passw']=$pword['password'];

                                   header("location:welc.htm");


                       }
                       else {

                       echo ("invalid username"."<br>");

                       }

       }

?>

</body>
</fieldset>
<br>For new user: Please, <a href="reg.php">click</a> here...</br>
</html>

This is Logion page code i started Session and saved session variables $_SESSION['User']=$uname['username']; and $_SESSION['Passw']=$pword['password'];..i want to display username in next page welc.php but it shows on first word of the username.what is problem here?

2
Contributors
4
Replies
38
Views
2 Years
Discussion Span
Last Post by broj1
0

What is the code in the welc.php?

Is $uname['username'] a username (usually just one word) or users real name (like first and last name)?

0
<?php session_start(); ?>

<html>

<head>
  <title>Welcome Page</title>
</head>
     <h1 style="width: 20%" >Login Page</h1>
       <fieldset style="width: 30%"> <legend style="background: #59CC57" >Login</legend>
<body style="background: #B6ECEB">
  <form action="login.php" method="post" >
  Username:<input type="text" name="username" />  <br></br>

  </form>

<?php

echo $_SESSION["User"];

?>

</body>
</fieldset>
<br>For new user: Please, <a href="reg.php">click</a> here...</br>
</html>

welc.php code.

1

On lines 26 and 27 you assign strings to $uname and $pword variables:

$uname = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$pword = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS);

On lines 38 and 39 you assign values to the $_SESSION but now you treat the above strings as arrays:

$_SESSION['User'] = $uname['username'];
$_SESSION['Passw'] = $pword['password'];

To me logical code would be:

$_SESSION['User'] = $uname;
$_SESSION['Passw'] = $pword;

except if you had something else in mind?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.