Jump to content

Yo!


Toy

Recommended Posts

I'm doing something for the fun of it but I've kinda reached a problem!

 

I have the login and index page on the same...eh, page, so I log in using the "login form" and it works fine, but I want it so that after i login the login form dissapears, I tried assigning a cookie to fix this but I can't go that way so yeah, help me!

Link to comment
Share on other sites

<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$password=md5($password);
$sql="SELECT xx FROM xx WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
{
echo 'This part is only visible if logged in!';
}
else
{
echo 'Sorry! The details you provided were incorrect, please try again...';
}
}
?>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
Username: 
<input type='text' name='username' class='input'>
Password: 
<input type='password' name='password' class='input'>
<input type='submit' value='Login' class='input'>
</form>

Link to comment
Share on other sites

<?php
session_start();

if(!isset($_SESSION['logged_in'])) {
$_SESSION['logged_in'] = false;
}

if($_SESSION['logged_in'] == false)
{
if($_SERVER["REQUEST_METHOD"] == "POST") {
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$password=md5($password);
$sql="SELECT xx FROM xx WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
{
$_SESSION['logged_in'] = true;
header("Location: ".$_SERVER['PHP_SELF']);
}
else
{
echo 'Sorry! The details you provided were incorrect, please try again...';
}
}
} else {
// Use it here instead
echo 'This part is only visible if logged in!';
}
?>

<?php if($_SESSION['logged_in'] == false) { ?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
Username: 
<input type='text' name='username' class='input'>
Password: 
<input type='password' name='password' class='input'>
<input type='submit' value='Login' class='input'>
</form>
<?php } ?>

 

In theory the above will work. It may need a little modification as I have not tested it.

Link to comment
Share on other sites

<?php
session_start();

if(!isset($_SESSION['logged_in'])) {
$_SESSION['logged_in'] = false;
}

if($_SESSION['logged_in'] == false)
{
if($_SERVER["REQUEST_METHOD"] == "POST") {
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$password=md5($password);
$sql="SELECT xx FROM xx WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1)
{
$_SESSION['logged_in'] = true;
header("Location: ".$_SERVER['PHP_SELF']);
}
else
{
echo 'Sorry! The details you provided were incorrect, please try again...';
}
}
} else {
// Use it here instead
echo 'This part is only visible if logged in!';
}
?>

<?php if($_SESSION['logged_in'] == false) { ?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
Username: 
<input type='text' name='username' class='input'>
Password: 
<input type='password' name='password' class='input'>
<input type='submit' value='Login' class='input'>
</form>
<?php } ?>

 

In theory the above will work. It may need a little modification as I have not tested it.

 

Thank you!

 

I now get a "Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs"

 

on line:

 

header("Location: ".$_SERVER['PHP_SELF']);

 

very tired right now, sry if I'm bothering you with something stupid

Link to comment
Share on other sites

one last thing, I'm having kind of a problem with echoing out the username or whatever value of the logged in user, I don't really know why, could someone provide a working snippet of code that does this, I'd be forever grateful?

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.