简体   繁体   中英

mysql value to html form

on my website i got a firebase chat running with username + PIN authentication required after logging in to the site and opening the chat box, (here is the most important three tags in this post:

    <div id='messagesDiv'></div>
    <input type='hidden' id='nameInput' value=''>
    <input type='text' id='messageInput' placeholder='Message'>

) i want the id='nameInput' value to be the username in the sql database. the sql structure is

Database:

"xxxx_usr"
User:
"xxxx_usr"
Host:
"Localhost"
Table:
"Users"
Table; 

Users:

------------------------------
id    | username  | password |
1     | Name      | ******   |
2     | Name2     | ******   |
------------------------------

and the end of the login script is

    session_register("myusername");
    session_register("mypassword"); 
    header("location:sucsess.html");

so is there any way to use this

    session.register("myusername")

to change

    <input type='hidden' id='nameInput' value=''>

value to the php registered username two pages earlier let's say Name so all chat messages the logged in user will send comes from Name.

and what should the complete controll.php be? now it looks like

after suggested edits it looks like

<?php

$host="localhost"; // Host name 
$username="*******_usr"; // Mysql username 
$password="*******"; // Mysql password 
$db_name="*******_usr"; // Database name 
$tbl_name="Users"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.
if (!isset ($_SESSION)) session_start();

$_SESSION["myusername"] = $username;
$_SESSION["mypassword"] = $password;
header("Location:sucsess.html");
}
else {
echo "Wrong Username or Password";
}
?>

becuase after the edits i can accsess sucsess.html without logging in or may it be the logout code that is wrong?

<?php
session_destroy();
?<

or the code that redirects non authhorisised users?

<?php
session_start();
if(!session_is_registred(myusername)) {
header("Location:index.html")
}
?>

Thank you for your answer

In order to use session variables use $_SESSION array instead of session_register() which has been deprecated.

//start session if it has not been started.
if (!isset ($_SESSION)) session_start(); 


$_SESSION["myusername"] = $username; //comes form db
$_SESSION["mypassword"] = $password;  //from db

And you can call where ever session values

<input type="hidden" id="nameInput" value="<?php echo $_SESSION['myusername'] ?>">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM