I am trying to let my website create a cookie on the user's system so that it will know if the user is logged in or out. But it doesnt seem to work.
Here is my javascript cookie code:
function createCookies()
{
// Creates a cookie on the user's computer if they login
var loginstate = true;
document.cookie = "login_state = " + loginstate;
}
function checkCookies()
{
// Check to see if there are cookies on the user's computer
var c_value = document.cookie;
if (c_value == "login_state = true") {
document.getElementById("loginstate").innerHTML = "Create Account";
document.getElementById("loginstate").setAttribute("href", "createaccount.aspx");
}
else if(c_value == "login_state = false")
{
document.getElementById("loginstate").innerHTML = "Logout";
document.getElementById("loginstate").setAttribute("href", "logout.html");
}
}
function createFalseCookies()
{
var loginstate = false;
document.cookie = "login_state = " + loginstate;
}
Here is my login page html and asp.net code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Geek's Lounge - Login</title>
<link rel="Stylesheet" href="styles/geeksloungestyle.css"/>
<!-- script section -->
<script type="text/javascript" src="scripts/cookies.js"></script>
<script type="text/javascript">
function failLogin()
{
alert("Error: Wrong email or password.");
}
</script>
<!-- script section end -->
<!-- meta section -->
<meta name="creation_date" content="31/10/2013"/>
<meta name="last_updated" content="31/10/2013"/>
<meta name="keywords" content="geek, lounge, computers, programming"/>
<!-- meta section end -->
</head>
<body onload="checkCookies();" background="pictures/wallpaper.jpg">
<form id="mainform" runat="server">
<div id="header">
<h1 id="headertext"><acronym title="cool programmers">Geek's</acronym> Lounge - Login</h1>
<!–– A place where geeks meet and are able to be geekish ––>
</div>
<br />
<br />
<br />
<div id="loginform">
<center>
Account Email: <asp:TextBox ID="loginemail" runat="server"></asp:TextBox> <br /> <br />
Account Password: <asp:TextBox ID="loginpassword" runat="server"></asp:TextBox> <br /> <br />
<asp:Button ID="submitbutton" runat="server" Text="Login" Width="80px" OnClick="submitbutton_Click"/>
</center>
</div>
</form>
</body>
</html>
ASP.NET of the login page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace geekslounge.geekslounge
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitbutton_Click(object sender, EventArgs e)
{
const string filepath = "members/";
string email = "";
string password = "";
int numberOfPosts = 0;
string name = "";
string surname = "";
string birthdate = "";
string homePage = "";
bool isCorrect = false;
string navURL = "";
email = loginemail.Text;
password = loginpassword.Text;
StreamReader file = new StreamReader("C:\\Users\\User\\Desktop\\geekslounge\\geekslounge\\geekslounge\\database.ini");
string line;
string []parts;
while((line = file.ReadLine()) != null)
{
parts = line.Split(';');
if (parts[0] == email && parts[1] == password)
{
numberOfPosts = Convert.ToInt32(parts[2].ToString());
name = parts[3];
surname = parts[4];
birthdate = parts[5];
homePage = parts[6];
isCorrect = true;
// Breakes out the loop.
break;
}
}
file.Close();
// Load the user's profile page and display their information for them.
if (isCorrect == true)
{
string jsMethodName = "createCookies()";
navURL = filepath + email + password + ".aspx";
Response.Redirect(navURL);
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);
}
// Else make a popup alert.
else
{
string jsMethodName = "failLogin()";
ScriptManager.RegisterClientScriptBlock(this, typeof(string), "uniqueKey", jsMethodName, true);
}
}
}
}
and then here is my logout code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="geekslounge/scripts/cookies.js"></script>
</head>
<body onload="createFalseCookies();">
<!-- nothing to be done here -->
</body>
</html>
Is there a better way to do this? And then how do I fix what is wrong with myne?