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
Dark_AngeL
Forum Commoner
Posts: 39 Joined: Tue Feb 21, 2006 5:16 am
Post
by Dark_AngeL » Wed Mar 01, 2006 12:04 am
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
But it gives me this error
Welcome
Notice: Undefined variable: name in
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Mar 01, 2006 12:07 am
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 » Wed Mar 01, 2006 12:09 am
explain more please
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Mar 01, 2006 12:11 am
Add this to your code.
Code: Select all
session_start()
$_SESSION['name'] = $name;
Then on your welcome page.
Put this
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 » Wed Mar 01, 2006 12:18 am
Shall i put them all in the main.php
or
should be in the check_login.php
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Mar 01, 2006 12:23 am
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 » Wed Mar 01, 2006 12:32 am
YES
Thank you sooooooooooooooooooooooooooooo much
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Mar 01, 2006 12:36 am
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.