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>
            &#60;&#33;&#8211;&#8211; A place where geeks meet and are able to be geekish &#8211;&#8211;&#62;
        </div>

        <br />
        <br />
        <br />

        <div id="loginform">
            <center>
                Account Email:&nbsp;&nbsp;&nbsp; <asp:TextBox ID="loginemail" runat="server"></asp:TextBox> <br /> <br />
                Account Password:&nbsp;<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?

I see that you are developing in asp.net. I dont want to steer you in another direction if you are trying to do this client side, but you may consider handling the cookies server side? Also, setting your login hyperlink stuff will be easy as well. I'm only suggesting this because if the client has javascript disabled, the user's experience will be different when accessing the site.

you may want to read over this: ASP.NET Cookies Overview

How this all changes your code...its not that significant. I suspect that the stuff you are trying to do client side with the login related components will be performed in your code-behind in the Page_Load subroutine.

Also, in your .aspx page, I dont see a control of any sort, label for example with an id of "loginstate" unless I missed it.

Good point about javascript being disabled. The asp.net cookies will work. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.