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?

Recommended Answers

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:


       
Jump to Post

All 4 Replies

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)?

<?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.

how can i display full name?

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.