Jump to content

cookies/session setting help


ksun

Recommended Posts

I have a problem with my php code on setting sessions and cookies for logins

i have 4 pieces of code so far

<?php
session_start();
$hostname = "##########";
$username = "##########";
$password = "###########";
$db_name = "############";
mysql_connect("$hostname","$username","$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select database");
function loggedin()
{
if(isset($_COOKIE['username']) || isset($_SESSION['username']) )
{
$loggedin = true;
return $loggedin;
="submit" name="butto

}

}


?>

config.php

include to connect to database and use function

 

 




<?php
include('config.php');
if(loggedin()){
header('Location: membersarea.php' );
}
?>
<html>
<body>
<form id="form1" name="form1" method="post" action="login.php">
<p>
<label for="textarea"></label>
<label for="textfield"></label>
</p>
<p> </p>
<p>Login</p>
<p>username:
<input type="text" name="username" id="textfield" />
</p>
<p>
<label for="textfield"></label>
password:
<input type="text" name="password" id="textfield" />
</p>
<p> rememberme
<input name="rememberme" type="checkbox" value="" />
<input typen" id="button" value="Submit" />
 </p>
</form>
</body>
</html>

login.html

 

 

<?php
include('config.php');
$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];
session_start();

if($username&&$password)
{
$login = mysql_query("SELECT * FROM user WHERE username = '$username'");
while($row = mysql_fetch_assoc($login))
{

$db_password = $row['password'];

if($password == $db_password){

 echo "success";
 $loginok = true;
}

}
}
else
{
die("enter both fields.");

}

if($loginok == true)
{
if($rememberme == "on")
{
setcookie("username", $username, time()+7200);
}
else if($rememberme == "")
{
$_SESSION['username'] = $username;
}
header("location:membersarea.php");
}
else
{

 $loginok = false;
 die("wrong password");
}


?>

 

login.php

 

<?php
include('config.php');
?>
<a href="logout.php">LOG OUT NOW</a>

 

memberspage.php

 

 

the main problem is when i goto login.html it instantly goes to memberspage even before i set session.

if anyone can help out that would be gr8

Link to comment
Share on other sites

That is just some mind boggling code. First of all, how is it that your system runs a login.html with php code in it? Did you configure your system to parse all your html files as php scripts? That certainly is not the default, and unless you made some addhandler configuration, that is certainly one of the many questions your current code brings to mind.

 

I'm guessing that some of the code got mangled when you pasted it, but your config.php comments are not valid php.

 

Certainly this is highly suspect code:

 

if(isset($_COOKIE['username']) || isset($_SESSION['username']) )
{
   $loggedin = true;

 

so if I have a cookie named username, then I login? That's obviously not what you want. With that said, it appears we're missing code, as you setup a database connection that is never used.

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.