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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.