Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have 2 pages 1- login.html (that connect to the "login.php")
2- home .html

i tried very much that show the username of the user when he has successful logging in
but i cant ):


HTML
the login form in login.html 
<!--LOGIN FORM-->
form name="login-form" class="login-form" action="login.php" method="post">

<!--HEADER-->
    <div class="header">
    <!--TITLE--><h1>Login Form</h1><!--END TITLE-->
    </div>
    <!--END HEADER-->
	
	<!--CONTENT-->
<div class="content">
   <!--USERNAME-->
<input name="user" type="text" class="input username"     value="Username" önfocus="this.value=''" />
   <!--END USERNAME-->

     <!--PASSWORD-->
<input name="pass" type="password" class="input password" value="Password"  önfocus="this.value=''" /><!--END PASSWORD-->
    </div>
    <!--END CONTENT-->
    
    <!--FOOTER-->
    <div class="footer">
         <!--LOGIN BUTTON-->
	<input type="submit" name="submit" value="Login" class="button" />
	<!--END LOGIN BUTTON-->
	
    <!--REGISTER BUTTON-->

        <a >Register</a>
	<!--END REGISTER BUTTON-->
	

	
	
    </div>
  
</form>
<!--END LOGIN >



login.php

PHP
<?php

define('DB_HOST', 'localhost');

define('DB_NAME', 'mydb');

define('DB_USER','root');

define('DB_PASSWORD','');

 

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());

$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

/*

$ID = $_POST['user'];

$Password = $_POST['pass'];

*/

function SignIn()

{

session_start();   //starting the session for user profile page

if(!empty($_POST['user']))   //checking the 'user' name which is from Sign-In.html, is it empty or have some text

{

    $query = mysql_query("SELECT *  FROM websiteusers where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());

    $row = mysql_fetch_array($query) or die(mysql_error());

    if(!empty($row['userName']) AND !empty($row['pass']))

    {

        $_SESSION['userName'] = $row['pass'];

        echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
        mysql_query("UPDATE websiteusers SET login=1 WHERE userName = '$_POST[user]' AND   pass = '$_POST[pass]'");
	    header('Location: home.html');

 

    }

    else

    {

        echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";

    }

}

}

if(isset($_POST['submit']))

{

    SignIn();

}

 

?>



how can i shwo the username in a text in home page ):
thanks
Posted
Comments
Killzone DeathMan 7-Feb-14 9:43am    
Why you put the password on the "userName" variable? "$_SESSION['userName'] = $row['pass'];"
Its easy: "<input type="text" value="<?php echo $_SESSION['userName']; ?>" />
Member 10564440 8-Feb-14 10:09am    
how can i send the current username to the "<input type="text" value="">" by the php ):
Killzone DeathMan 11-Feb-14 8:29am    
Sorry, I updated my comment! :P
Any question add me on skype :)

Looks like you are already setting the username into a session variable, and to be able to print that on a different page than the page you want it printed on, needs to be *.php instead of *.html

So you would have to change home.html to home.php so you would be able to use PHP there, and then you would simply echo the $_SESSION['userName'] to display the user name!!! The problem in your code there though is you have $row['pass'] being set to $_SESSION['userName'].

Then in index.php you would echo the $_SESSION['userName'] where you want the username to appear.

You can use the shorthand method to echo the $_SESSION['userName'] (just make sure this is enabled in Apache)
PHP
<?=$_SESSION['userName'];?>

or you can use the normal method. Neither is faster as far as I know.
PHP
<?php echo $_SESSION['userName'];?>


Also, you are not sanitizing/validating the user submitted values. Stack overflow has a solution for this... a pretty detailed one. Check it out for your data validation and sanitation.
 
Share this answer
 
v2
u have not aligned display to $id for display
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900