Post: More PHP Login's! (HELP NEEDED)
11-27-2011, 10:25 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I have a login/logout system. Im trying to have a "keep logged in" thing, but when I set a cookie I recieve an error (header already sent) can you help?

login.php

    
<?php include('/home/***/public_html/admincp/server.php'Winky Winky; ?>
<?php

// Connect to server and select databse.
mysql_connect("$db_host", "$db_user", "$db_pass")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

SESSION_ID('login'Winky Winky;
SESSION_START('login'Winky Winky;

$username = $_POST['username'];
$password = sha1(md5(sha1(sha1($_REQUEST['password']))));

echo "<form method='post' name='loginform'>
<label>Username</label>
<input name='username' type='text' />
<label>Password</label>
<input name='password' type='password' />
<input name='remember' type='checkbox' />
<label>Remember Me</label>
<input name='submit' type='submit' value='login'/>
</form>";

//protection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

//connect to the database here
$sql="SELECT * FROM members WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
//pass check
$checkpass="SELECT password FROM members WHERE username='$username'";

//login script
if($_SERVER['REQUEST_METHOD'] == "POST")
{

if($count==1)
{
$_SESSION['valid'] = 1;//sets the session data for this user
$_SESSION['username'] = $username;//sets session data username for later use.
echo "Login Success!";

if(isset($_POST['remember']) &&
$_POST['remember'] == 'Yes'Winky Winky
{
setcookie("mycookie", "mycookie", time()+3600);
}else{
setcookie("mycookie", "mycookie", time()-3600);
}
echo "<meta http-equiv='REFRESH' content='2;url=https://www.****/admincp/index.php'>";
}else
{
echo "<meta http-equiv='REFRESH' content='2;url=https://www.****/admincp/login.php'>";
echo "Login Failed!";
}
}
?>



index.php

    
<?php
//starts session for users not logged in
session_start();
//checks if logged in function
function isLoggedIn()
{
if(isset($_SESSION['valid']) && $_SESSION['valid'])
return true;
return false;
}

if(isset($_COOKIE['mycookie']))
{
$_SESSION['valid'] = 1;
}

if(!isLoggedIn())
{
echo "You are not logged in, please login to continue<br /><br /><a href='https://www.*****/admincp/login.php'>Login</a>";
}else{
echo "Welcome to the Administration Panel, " .$_SESSION['username']. "!<br /><a href='https://www.***/admincp/logout.php'>logout</a>";
}

?>



logout.php

    
<?php
setcookie("mycookie", "mycookie", time()-3600);
SESSION_START();
$_SESSION = array(); //destroy all of the session variables
session_destroy();
echo "<meta http-equiv='REFRESH' content='0;url=https://www.****.co.cc/admincp/login.php'>";
?>

(adsbygoogle = window.adsbygoogle || []).push({});
11-27-2011, 10:51 PM #2
Is your SQL connecting properly?
11-28-2011, 01:14 AM #3
Originally posted by LordOfSpoon
Is your SQL connecting properly?


yeah I fixed that script, it's working. But I would like to create a cookie (not a session cookie) so It automatically logs in. Any idea how I could do that?
11-28-2011, 05:50 AM #4
Epic?
Awe-Inspiring
Originally posted by Team
yeah I fixed that script, it's working. But I would like to create a cookie (not a session cookie) so It automatically logs in. Any idea how I could do that?


Cookies can be stored in the $_COOKIE superglobal array, you can use setcookie() to set a cookie. You might want to read: You must login or register to view this content.

Oh, and you really ought to consider buying a book, or at least trying to learn the language before you ask for help on such simple things.
11-28-2011, 10:30 PM #5
Originally posted by Epic
Cookies can be stored in the $_COOKIE superglobal array, you can use setcookie() to set a cookie. You might want to read: You must login or register to view this content.

Oh, and you really ought to consider buying a book, or at least trying to learn the language before you ask for help on such simple things.



/facepalm

look in all 3 of my scripts. I have setcookie() I know how to use it, it's not working for some reason, look for yourself. Please do not insult me before looking at what I have provided. Thanks Smile
11-28-2011, 10:39 PM #6
Epic?
Awe-Inspiring
Originally posted by Team
/facepalm

look in all 3 of my scripts. I have setcookie() I know how to use it, it's not working for some reason, look for yourself. Please do not insult me before looking at what I have provided. Thanks Smile


I'm not insulting you, just based on what you've said over the past while, you clearly don't know a lot about programming right now, which is why I'd recommend you purchase a book, learn how to program, then come back and try again later.
11-28-2011, 11:03 PM #7
Originally posted by Epic
I'm not insulting you, just based on what you've said over the past while, you clearly don't know a lot about programming right now, which is why I'd recommend you purchase a book, learn how to program, then come back and try again later.


stare, I know programming very well actually, just not php cause I barely use it. I have learned alot since last time, but this is not working. I setcookie() like I would any other script, but I don't think it is because of the checkbox. Can you look at the if(isset($_POST['remember']) && $_POST['remember']=='Yes' {do setcookie()}

I don't think that's right, I never really used checkboxes in php before.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo