Jump to content

Sessions to Cookies


Helminthophobe

Recommended Posts

I've got a site setup with user accounts. I used a tutorial online to get the account system to work, which I can't seem to find again. It uses sessions, which of course causes issues for those that don't like to be required to login every time they visit the site. I've never used cookies before and as such I have no clue how I can change sessions to cookies. Can anyone help me?

 

This is the code at the top of all my pages:

<?
session_start();
include "conn.inc.php";

if ($mode == "logout")
{
$_SESSION['user_name'] = "" . $_SESSION['user_password'] = "";
}

if ((isset($_SESSION['user_name']) && $_SESSION['user_name'] != "") || (isset($_SESSION['user_password']) && $_SESSION['user_password'] != ""))
{
$logged = "logged";
}
else
{
$logged = "unlogged";
}
?>

 

This is the login code and the way I am displaying content on other pages:

<?
if ($logged == "logged")
{
if ($row1['active'] == 0)
	{
	echo "<p class=\"title\">OGame Database Login and Tools</p>";
	echo "<p class=\"body\">You are logged in " . $row1['user_name'] . ".</p>";
	echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
	include "includes/new.php";
	}
	else
	{
	echo "<p class=\"title\">OGame Database Login and Tools</p>";
	echo "<p class=\"body\">You are logged in " . $row1['user_name'] . ".</p>";
	echo "<p class=\"body\">Use the icons below the Home icon on the menu to continue navigating the site. You can view or edit your user account information, view or edit your OGame account information, or simply check out some data.</p>";
	echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
	include "includes/new.php";
	}
}
else
{

if (isset($_POST['submit'])) {
$query = "SELECT user_name, password FROM user " .
	"WHERE user_name = '" . $_POST['user_name'] . "' " .
	"AND password = (PASSWORD('" . $_POST['password'] . "'))";
$result = mysql_query($query) 
or die(mysql_error());

if (mysql_num_rows($result) == 1) {
	$_SESSION['user_name'] = $_POST['user_name'];
	$_SESSION['user_password'] = $_POST['password'];
	header ("Refresh: 2; URL=" . $_POST['redirect'] . "");
	echo "<span class=\"body\">You will be redirected in 2 seconds.<br />(If your browser doesn't support this, <a href=\"" . $_POST['redirect']. "\">click here</a>)</span>";
	}
	else
	{
	echo "<p class=\"title\">OGame Database Login and Tools</p>";
	echo "<p class=\"body\">The OGame Database is still in development. You may register and login, but you can't access the database yet. Please check back soon.</p>";
	echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
	include "includes/new.php";

	echo "<span class=\"body\"><font color=\"#FF0000\">Invalid Username and/or Password</font><br />Not registered? <a href=\"register.php\">Click here</a> to register.<br /><br /></span>";
	echo "<form action=\"index.php\" method=\"post\"><table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"body\">";
	echo "<tr>";
	echo "<td width=\"20%\">Username:</td>";
	echo "<td><input type=\"text\" name=\"user_name\" class=\"form_format\"></td></tr>";
	echo "<tr>";
	echo "<td width=\"20%\">Password:</td>";
	echo "<td><input type=\"password\" name=\"password\" class=\"form_format\"></td></tr>";
	echo "<tr><td colspan=\"2\" align=\"center\" valign=\"middle\"><input type=\"submit\" name=\"submit\" value=\"Login\" class=\"form_button\"></td>";
	echo "</tr></table></form>";
	}
}
else
{
if (isset($_GET['redirect'])) {
	$redirect = $_GET['redirect'];
	}
	else
	{
	$redirect = "index.php";
	}
echo "<p class=\"title\">OGame Database Login and Tools</p>";
echo "<p class=\"body\">The OGame Database is still in development. You may register and login, but you can't access the database yet. Please check back soon.</p>";
echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
include "includes/new.php";

echo "<form action=\"index.php\" method=\"post\"><table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"body\">";
echo "<tr>";
echo "<td width=\"20%\">Username:</td>";
echo "<td><input type=\"text\" name=\"user_name\" class=\"form_format\"></td></tr>";
echo "<tr>";
echo "<td width=\"20%\">Password:</td>";
echo "<td><input type=\"password\" name=\"password\" class=\"form_format\"></td></tr>";
echo "<tr><td colspan=\"2\" align=\"center\" valign=\"middle\"><input type=\"submit\" name=\"submit\" value=\"Login\" class=\"form_button\"></td>";
echo "</tr></table></form>";
}
}
?>

 

I'm very new to PHP so forgive me if the code has errors or could be done in a more simple manner. Thank you for any assistance provided.

Link to comment
Share on other sites

Try this: (I've included a nice dropdown with some options for how long or 'don't remember me')

 

The Login:

<?
if ($logged == "logged")
{
if ($row1['active'] == 0)
	{
	echo "<p class=\"title\">OGame Database Login and Tools</p>";
	echo "<p class=\"body\">You are logged in " . $row1['user_name'] . ".</p>";
	echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
	include "includes/new.php";
	}
	else
	{
	echo "<p class=\"title\">OGame Database Login and Tools</p>";
	echo "<p class=\"body\">You are logged in " . $row1['user_name'] . ".</p>";
	echo "<p class=\"body\">Use the icons below the Home icon on the menu to continue navigating the site. You can view or edit your user account information, view or edit your OGame account information, or simply check out some data.</p>";
	echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
	include "includes/new.php";
	}
}
else
{

if (isset($_POST['submit'])) {
$query = "SELECT user_name, password FROM user " .
	"WHERE user_name = '" . $_POST['user_name'] . "' " .
	"AND password = (PASSWORD('" . $_POST['password'] . "'))";
$result = mysql_query($query) 
or die(mysql_error());

if (mysql_num_rows($result) == 1) {
	$uname = $_POST['user_name'];
	$upass = $_POST['password'];
	$sessiontime = "time()+".$_POST['login_time'];
	setcookie("user_name", $uname, $sessiontime);
	setcookie("user_password", $upass, $sessiontime);

	header ("Refresh: 2; URL=" . $_POST['redirect'] . "");
	echo "<span class=\"body\">You will be redirected in 2 seconds.<br />(If your browser doesn't support this, <a href=\"" . $_POST['redirect']. "\">click here</a>)</span>";
	}
	else
	{
	echo "<p class=\"title\">OGame Database Login and Tools</p>";
	echo "<p class=\"body\">The OGame Database is still in development. You may register and login, but you can't access the database yet. Please check back soon.</p>";
	echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
	include "includes/new.php";

	echo "<span class=\"body\"><font color=\"#FF0000\">Invalid Username and/or Password</font><br />Not registered? <a href=\"register.php\">Click here</a> to register.<br /><br /></span>";
	echo "<form action=\"index.php\" method=\"post\"><table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"body\">";
	echo "<tr>";
	echo "<td width=\"20%\">Username:</td>";
	echo "<td><input type=\"text\" name=\"user_name\" class=\"form_format\"></td></tr>";
	echo "<tr>";
	echo "<td width=\"20%\">Password:</td>";
	echo "<td><input type=\"password\" name=\"password\" class=\"form_format\"></td></tr>";
	echo "<tr>";
	echo "<td width=\"20%\">Login Time:</td>";
	echo "<td>
				<select name=\"login_time\" class=\"form_format\">
					<option value=\"600\">10 Minutes</option>
					<option value=\"3600\">1 Hour</option>
					<option value=\"43200\">12 Hours</option>
					<option value=\"86400\">A Day</option>
					<option value=\"1209600\">Two Weeks</option>
					<option value=\"0\" selected>Don't remember me</option>
				</select>
		</td></tr>";
	echo "<tr><td colspan=\"2\" align=\"center\" valign=\"middle\"><input type=\"submit\" name=\"submit\" value=\"Login\" class=\"form_button\"></td>";
	echo "</tr></table></form>";
	}
}
else
{
if (isset($_GET['redirect'])) {
	$redirect = $_GET['redirect'];
	}
	else
	{
	$redirect = "index.php";
	}
echo "<p class=\"title\">OGame Database Login and Tools</p>";
echo "<p class=\"body\">The OGame Database is still in development. You may register and login, but you can't access the database yet. Please check back soon.</p>";
echo "<p class=\"body\">There are a number of tools available on the site that range from calculators to report formatters. You can access each by clicking one of the icons below \"TOOLS\" on the menu.</p>"; 
include "includes/new.php";

echo "<form action=\"index.php\" method=\"post\"><table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"body\">";
echo "<tr>";
echo "<td width=\"20%\">Username:</td>";
echo "<td><input type=\"text\" name=\"user_name\" class=\"form_format\"></td></tr>";
echo "<tr>";
echo "<td width=\"20%\">Password:</td>";
echo "<td><input type=\"password\" name=\"password\" class=\"form_format\"></td></tr>";
echo "<tr>";
	echo "<td width=\"20%\">Login Time:</td>";
	echo "<td>
				<select name=\"login_time\" class=\"form_format\">
					<option value=\"600\">10 Minutes</option>
					<option value=\"3600\">1 Hour</option>
					<option value=\"43200\">12 Hours</option>
					<option value=\"86400\">A Day</option>
					<option value=\"1209600\">Two Weeks</option>
					<option value=\"0\" selected>Don't remember me</option>
				</select>
		</td></tr>";
echo "<tr><td colspan=\"2\" align=\"center\" valign=\"middle\"><input type=\"submit\" name=\"submit\" value=\"Login\" class=\"form_button\"></td>";
echo "</tr></table></form>";
}
}
?>

 

The Access Check script on each page:

<?
include "conn.inc.php";

if ($mode == "logout")
{
$_COOKIE['user_name'] = "" . $_COOKIE['user_password'] = "";
}

if ((isset($_COOKIE['user_name']) && $_COOKIE['user_name'] != "") || (isset($_COOKIE['user_password']) && $_COOKIE['user_password'] != ""))
{
$logged = "logged";
}
else
{
$logged = "unlogged";
}
?>

Link to comment
Share on other sites

I appreciate the help but it doesn't seem to work. Normally when you log in, the script reloads the page and based on whether or not the account is activated, you get certain content. When I use the code you posted, the page reloads like it's suppose to but it displays the login form again. I don't understand how the code works so I don't know how to troubleshoot it. Any thoughts?

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.