/    Sign up×
Community /Pin to ProfileBookmark

Password protect a page

I want to password protect a page. Thats why I have created this script. The only problem is that after login I have to reftesh the window one time to hide the login window and see the contents of the page.

Here is the script

[code]
<?php
session_start();
if($_SESSION[‘logedin’] == ‘vmesid’)
{
echo “This is the content”;
}
?>
<?php
session_start();
if($_SESSION[‘logedin’] != ‘vmesid’)
{
echo “<html>”;
echo “<body>”;
echo “<form method=post>”;
echo “<center><table>”;
echo “<tr><td>Enter Password Protected Area</td>”;
echo “</tr><tr><td>Username: <input type=text name=username size=15></td>”;
echo “</tr><tr><td>Password: <input type=password name=password size=15></td>”;
echo “</tr><tr><td><input name=Submit type=submit value=Login></td></tr></table>”;
echo “</form>”;
echo “</body>”;
echo “</html>”;
}
if ($_POST[‘Submit’]) {
if($_POST[‘username’] == ‘123’ && $_POST[‘password’] == ‘123’ )
{
session_start();
$_SESSION[‘logedin’] = ‘vmesid’;
}
}
}
?>

[/code]

What could be wrong with script ???

Any suggestion !

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ExuroMar 23.2005 — I think you need to move the session variable setting part up above the page generation, that way it sets the session variable, then when it's generating the content it can see it. It would also be a good idea to use the [FONT=courier new]isset()[/FONT] method to check if the indecies are set before you try and access them. I removed a couple [FONT=courier new]session_start()[/FONT] calls too because I don't think they were necessary. However, if it doesn't work right for you, you can always add them back in. Anyway, here you go:
[code=php]<?php
if (isset($_POST['Submit'])) {
if($_POST['username'] == '123' && $_POST['password'] == '123' )
{
session_start();
$_SESSION['logedin'] = 'vmesid';
}
}
if(isset($_SESSION['logedin']) && $_SESSION['logedin'] == 'vmesid')
{
echo "This is the content";
}
?>
<?php
if(!isset($_SESSION['logedin']) || $_SESSION['logedin'] != 'vmesid')
{
echo "<html>";
echo "<body>";
echo "<form method=post>";
echo "<center><table>";
echo "<tr><td>Enter Password Protected Area</td>";
echo "</tr><tr><td>Username: <input type=text name=username size=15></td>";
echo "</tr><tr><td>Password: <input type=password name=password size=15></td>";
echo "</tr><tr><td><input name=Submit type=submit value=Login></td></tr></table>";
echo "</form>";
echo "</body>";
echo "</html>";
}
?>[/code]
×

Success!

Help @saud_iqbal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...