Jump to content

php login


dezkit

Recommended Posts

is there a way so i can make that so if the person isn't logged in in index.php, they can't view the page.

 

here is the code


<?php
$username = "lemon";
$password = "lemon";
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<form name="login" method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<table>
<tr><td>Username:
<td><input type="text" title="Enter your Username" name="txtUsername" />
<tr><td>Password:
<td><input type="password" title="Enter your password" name="txtPassword" />
<tr><td colspan=2 align=right><input type="submit" name="Submit" value="Login" />
</table>
</form>
<?php
}
else {
?>

<?php 
echo "Hello ";
echo "$username";
echo ".<br>You will be redirected to the admin page in a couple of seconds...";
?>

<META http-equiv="refresh" content="2;URL=http://www.elementgaming.org/admin/">
<?php
}
?> 

[/code

Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/
Share on other sites

ok,

at the top of everypage put session_start();

 

put this on the login page for when they login

$_SESSION['logged_in] = true;

 

then on the index page put

if ($_SESSION['logged_in] == true)

{

// what ever the index page is

}

else

{

//what ever if there not logged in

}

 

Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/#findComment-524335
Share on other sites

alright, i didn't go to the gym, it was closed, any who...

this is the code that i got (it doesn't work)

<?php
$username = "lemon";
$password = "alexj";
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<form name="login" method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<table>
<tr><td>Username:
<td><input type="text" title="Enter your Username" name="txtUsername" />
<tr><td>Password:
<td><input type="password" title="Enter your password" name="txtPassword" />
<tr><td colspan=2 align=right><input type="submit" name="Submit" value="Login" />
</table>
</form>
<?php
}
else {
?>

<?php
session_start();
$_SESSION['element_gaming_logged_in'] = true;
echo "Hello ";
echo "$username";
echo ".<br>You will be redirected to the admin page in a couple of seconds...";
?>

<META http-equiv="refresh" content="2;URL=http://www.elementgaming.org/admin/">
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/#findComment-524385
Share on other sites

this is the error code, Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/elementg/public_html/site/index.php:7) in /home/elementg/public_html/site/index.php on line 114

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/elementg/public_html/site/index.php:7) in /home/elementg/public_html/site/index.php on line 114

 

line 114 is session_start();

Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/#findComment-524390
Share on other sites

i want the session to start AFTER they login.

 

index.php


<?php
$username = "lemon";
$password = "123";
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<form name="login" method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<table>
<tr><td>Username:
<td><input type="text" title="Enter your Username" name="txtUsername" />
<tr><td>Password:
<td><input type="password" title="Enter your password" name="txtPassword" />
<tr><td colspan=2 align=right><input type="submit" name="Submit" value="Login" />
</table>
</form>
<?php
}
else {
?>

<?php
session_start();
$_SESSION['logged_in'] = true;
echo "Hello ";
echo "$username";
echo ".<br>You will be redirected to the admin page in a couple of seconds...";
?>

<META http-equiv="refresh" content="2;URL=http://www.elementgaming.org/site/admin/">
<?php
}
?>


 

/admin/index.php


<?php


if ($_SESSION['logged_in'] == true){
?>
You are logged in.
<?php
}
else
{
?>
You are not logged in.
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/#findComment-524405
Share on other sites

i get no error messaged and yeah i have session_start();

 

index.php


<?php
$username = "lemon";
$password = "123";
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<form name="login" method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<table>
<tr><td>Username:
<td><input type="text" title="Enter your Username" name="txtUsername" />
<tr><td>Password:
<td><input type="password" title="Enter your password" name="txtPassword" />
<tr><td colspan=2 align=right><input type="submit" name="Submit" value="Login" />
</table>
</form>
<?php
}
else {
?>

<?php

session_start();
$_SESSION['logged_in'] = yes;

echo "Hello ";
echo "$username";
echo ".<br>You will be redirected to the admin page in a couple of seconds...";

?>

<META http-equiv="refresh" content="2;URL=/admin/">
<?php
}
?> 

 

 

/admin/index.php


<?php
session_start();
if ($_SESSION['logged_in'] == yes){
?>
You are logged in.
<?php
}
else
{
?>
You are not logged in.
<?php
}
?>


Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/#findComment-524425
Share on other sites

try this

<?php
session_start();
if ($_SESSION['logged_in'] == true){
echo "You are logged in.";
}
else 
{
echo "You are not logged in.";
}
?>

and this

<?php
session_start();
$username = "lemon";
$password = "123";
if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?>
<form name="login" method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<table>
<tr><td>Username:
<td><input type="text" title="Enter your Username" name="txtUsername" />
<tr><td>Password:
<td><input type="password" title="Enter your password" name="txtPassword" />
<tr><td colspan=2 align=right><input type="submit" name="Submit" value="Login" />
</table>
</form>
<?php
}
else {


$_SESSION['logged_in'] == true;

echo "Hello ";
echo "$username";
echo ".<br>You will be redirected to the admin page in a couple of seconds...";

?>
<META http-equiv="refresh" content="2;URL=/admin/">
<?php
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/102399-php-login/#findComment-524433
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.