By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,718 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,718 IT Pros & Developers. It's quick & easy.

simple session login problem

P: n/a
I created a php page that will create a session for a user. My problem
is that once they are logged in, I dont know how to send them to the
secure page. I dont know where to place the following lines of code:
(also not sure where on the server the pages are placed)

header("Location: http://www.mysite.com/index.html");
exit;

Original code-------------------------------------------------
<?
session_start();
?>
<html>
<head>
<title> </title>
</head>
<body>
<?
$_Username = "user";
$_Password = "password";
// If the form was submitted
if ($_POST['Submitted'] == "True") {
// If the username and password match up, then continue...
if ($_POST['Username'] == $_Username && $_POST['Password'] ==
$_Password) {
// Username and password matched, set them as logged in and
set the
// Username to a session variable.
$_SESSION['Logged_In'] = "True";
$_SESSION['Username'] = $_Username;
}
}
if ($_SESSION['Logged_In'] != "True") {
echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] .
"\">
Username: <input type=\"textbox\" name=\"Username\"><br />
Password: <input type=\"textbox\" name=\"Password\"><br />
<input type=\"hidden\" name=\"Submitted\" value=\"True\">
<input type=\"Submit\" name=\"Submit\">
</form>";
}
else
{
echo "You are logged in as: <b>" . $_SESSION['Username'] . "</b>
<br /><a href=\"" . $_SERVER['PHP_SELF'] .
"?mode=logout\">Logout</a>";
}
if ($_GET['mode'] == "logout") {
session_start();
$_SESSION = array();
session_destroy();
echo "<META HTTP-EQUIV=\"refresh\" content=\"1; URL=" .
$_SERVER['PHP_SELF'] . "\">";
}
?>
<p>&nbsp;</p>
</body>
</html>
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 16 Oct 2004 12:42:34 -0700, em******@hotmail.com (Frank) wrote:
I created a php page that will create a session for a user. My problem
is that once they are logged in, I dont know how to send them to the
secure page. I dont know where to place the following lines of code:
(also not sure where on the server the pages are placed)

header("Location: http://www.mysite.com/index.html");
exit;


Place it right after you create the sessions upon successful user
login. You probably also want to check and verify the form variables
for security.

Anthony
Jul 17 '05 #2

P: n/a
If I place it after a sucessful login, I get ....
Warning: Cannot modify header information - headers already sent by
(output started at /my server path/my site.com/login1.php:9) in /my
server path/my site.com/login1.php on line 41
successful login code...SNIP>>>>>>>>>>>>>>>>>>>>>>>>>>
echo "You are logged in as: <b>" . $_SESSION['Username'] . "</b>
<br /><a href=\"" . $_SERVER['PHP_SELF'] .
"?mode=logout\">Logout</a>";
header("Location: http://www.mysite.com/index.html");
exit;
>>>>>>>>>>>>>>>>>>>>>>>

Anthony <ca*********@gmail.com> wrote in message news:<1097970490.76HQl5mnEbwbH2IbEhxF+w@teranews>. .. On 16 Oct 2004 12:42:34 -0700, em******@hotmail.com (Frank) wrote:
I created a php page that will create a session for a user. My problem
is that once they are logged in, I dont know how to send them to the
secure page. I dont know where to place the following lines of code:
(also not sure where on the server the pages are placed)

header("Location: http://www.mysite.com/index.html");
exit;


Place it right after you create the sessions upon successful user
login. You probably also want to check and verify the form variables
for security.

Anthony

Jul 17 '05 #3

P: n/a
Frank wrote:
If I place it after a sucessful login, I get ....
Warning: Cannot modify header information - headers already sent by
(output started at /my server path/my site.com/login1.php:9) in /my
server path/my site.com/login1.php on line 41
successful login code...SNIP>>>>>>>>>>>>>>>>>>>>>>>>>>
echo "You are logged in as: <b>" . $_SESSION['Username'] . "</b>
<br /><a href=\"" . $_SERVER['PHP_SELF'] .
"?mode=logout\">Logout</a>";
header("Location: http://www.mysite.com/index.html");
exit;


You have to sent headers before the content, and you already echoed the
login name. You can use buffered output (ob_* functions), or maybe
accumulate output in a variable and then echo it at the end (although I
don't see a readon why to output anything if you are redirecting anyway :))
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.