简体   繁体   中英

how can i allow only one visitor to visit the page

I have a page call "A" Asp.Net and i want to allow only one user to access the page. If nobody accessing the page then any user are allowed to access the page Without using database how can i do it. I am trying to use page load of c# and window onbeforeunload event of javascript but didnt get it.

<script type="text/javascript" language="JavaScript">
        var hidViewstate;

        window.onload = body_Load;
        window.onbeforeunload = WindowCloseHanlder;

        function body_Load()
        {
            hidViewstate = document.getElementById('<%=hidViewstate.ClientID %>');
        }

        function WindowCloseHanlder()
        {
            hidViewstate.value = parseFloat(hidViewstate.value - 1);
        }

    </script>

<div>
        <asp:Label ID="lblText" runat="server" Text="Label"></asp:Label>
        <asp:HiddenField ID="hidViewstate" runat="server" />
    </div>

protected void Page_Load(object sender, EventArgs e)
    {
        hidViewstate.Value = "1";

        if(Convert.ToInt32(hidViewstate.Value) == 1)
        {
            lblText.Text = "Welcome First User";
        }
        else
        {       
            lblText.Text = "Visiting on this page is not allowed because user is already login.";

        }

    }

This is counter to how a web server should operate. It would be interesting to know your actual problem that led you to try this.

On the technical side you could store a global variable in Application . All requests share the same global object.

But if you explained to us the real problem you are trying to solve I think you would find this isn't a good approach and Im sure we could suggest a real solution.

You can use a global veriable, like static variable of a static class,

on page load you can check like

if(clsGlobal.userCount > 1)
{
Response.Redirect("~/anotherPage.aspx") // somewhere you want..
}
else
{
//display your welcome message
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM