Welcome "username"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Welcome "username"

Post 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
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post by Dark_AngeL »

explain more please :oops:
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post 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
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post by Dark_AngeL »

YES :lol:


Thank you sooooooooooooooooooooooooooooo much
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Post Reply