Page 1 of 1

Welcome "username"

Posted: Wed Mar 01, 2006 12:04 am
by Dark_AngeL
Hi

I have a question .. Lets say the user logs in with the name = Heba and goes to the menu page

How can i welcome the user with his name

like Welcome Heba !

I did this

Code: Select all

Welcome <? echo "$name";?>
But it gives me this error

Welcome
Notice: Undefined variable: name in

Posted: Wed Mar 01, 2006 12:07 am
by nickman013
On the login page make a hidden field with the value of $name.

Or generate a session and store the name in it. Very easy.

Posted: Wed Mar 01, 2006 12:09 am
by Dark_AngeL
explain more please :oops:

Posted: Wed Mar 01, 2006 12:11 am
by nickman013
Add this to your code.

Code: Select all

session_start()
$_SESSION['name'] = $name;
Then on your welcome page.

Put this

Code: Select all

echo $_SESSION['name'];
EDIT:

Add it to where the part of your code where it makes sure the user exist.

Posted: Wed Mar 01, 2006 12:18 am
by Dark_AngeL
:roll: Shall i put them all in the main.php
or

Code: Select all

start session();
should be in the check_login.php

Posted: Wed Mar 01, 2006 12:23 am
by nickman013
Wait acutally I was wrong.

To pass the session to other pages, you need session_start() at the top.

Make this your login code.

Code: Select all

<?php   
error_reporting('E_ALL');    

   $denied = "";  
   $name = $_POST['name'];  
   $pass = $_POST['pass'];  
   if ($name == "Heba" &&  $pass == "123")  
   {   
       session_start()
       $_SESSION['name'] = $name;
       header("Location: main.php'');/* Redirect browser */  
       exit;                  
   }  
   else {  
       $denied = "Sorry. You are not authorized to access this page<br>Go <a href='index.php' onclick='history.go(-1);return false'> Back </a>";  
   }  
}  
?>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
<title>SYS</title>  
.  
.  
.  
.  
...  
.  
.  
.  
.  
.  
.  
<body>  
<?php  
if(!empty($denied))  
{  
echo $denied;  
}  
?>  
.  
.  


...  



.  
.  
.  
.  
.  
.  
.  
</body>  
</html>
And on main.php

add session_start() to the very top. and where you want the name to appear you must echo $_SESSION['name'].

Also. The isnt very secure. People can just go to main.php and go on. You should use sessions to secure it.

Posted: Wed Mar 01, 2006 12:32 am
by Dark_AngeL
YES :lol:


Thank you sooooooooooooooooooooooooooooo much

Posted: Wed Mar 01, 2006 12:36 am
by nickman013
No problem.

I think you should protect your pages. It is just like that, easy. It takes 2 minuetes to do.

Alls you have to do is add one line of code to your pages and it will protect people from going to them, they will have to have a session generated by the login page. If they dont, they get redirected to the login page or any page.