locked
How to start and end SESSION in web pages RRS feed

  • Question

  • User2072242043 posted

    HI,

    I Have a question which is very related to my last thread. but I decided to start a very new thread to clear my concepts which I have before in PHP.
    I used to use sessions in such a way which restrict user from inappropriate actions.
    I want to ask you that, in php i start a SESSION by calling this function 

    session_start();

     But Q1: what is the way to start and end session in asp.net Web pages?
    In php Whenever a i try to redirect a Page using session in if by this code:

    if (isset($_POST['submit']))
     {
    		
    				header('Location: superadmin.php');
    }

    it always did work for me.
    Q2:
    What should I use here in php in place of header to redirect it to the destination?
    I hope someone have better steps of guidance for me.
    Thanks 

    Wednesday, August 12, 2015 10:30 AM

Answers

  • User753101303 posted

    Hi,

    Q1: Assuming you are talking about session variables (rather than user session from a security point of view), in ASP.NET, the session is just available if enabled. And the session id will be fixed once you stored your first session variable.

    The session ends automatically when the user doesn't hit the site for 20 minutes (this is a default). See https://msdn.microsoft.com/en-us/library/ms178581.aspx for details.

    Q2: Seems unrelated to session but to just check if a submit button was posted to the page. Use Response.Redirect to go to another page (which likely does nothing else than sending a location header).

    Don't compare too much PHP and ASP.NET. You have a whole Framework so even if the underlying principles are the same it could be likely done quite differently in ASP.NET.

    If you were talking rather about authentication you have a whole part of ASP.NET to handle this for you with likely a quite steep learning curve but allowing to easily have this done to focus rarther on business needs once familiar with it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 11:13 AM
  • User2072242043 posted

    Thanks PatriceSc.
    I am very new here at asp.net and trying to learn things which comes in my experience on daily basis.
    as before i worked with php thats why i did show a similarity with asp.net in my question.
    IT means for redirecting i should only use response.redirect method than whatever i use in my conditions whether it is a session like

     if (Session["SessionName"] != null && Session["SessionRole"]=="Super Admin")
    	                    {
                                 Response.Redirect("~/superAdmin");    
                                	
                            }

    or simple IsPost?

    Is header is php function or a part of html?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 12:00 PM
  • User325035487 posted

    The code above should work. Instead of checking !=null in every page just write the null check in _PageStart.cshtml in root.

    if (Session["SessionName"] != null)
    - You Can then  restore the sesssion variable in the else part if it gets to be null

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 8:49 PM

All replies

  • User753101303 posted

    Hi,

    Q1: Assuming you are talking about session variables (rather than user session from a security point of view), in ASP.NET, the session is just available if enabled. And the session id will be fixed once you stored your first session variable.

    The session ends automatically when the user doesn't hit the site for 20 minutes (this is a default). See https://msdn.microsoft.com/en-us/library/ms178581.aspx for details.

    Q2: Seems unrelated to session but to just check if a submit button was posted to the page. Use Response.Redirect to go to another page (which likely does nothing else than sending a location header).

    Don't compare too much PHP and ASP.NET. You have a whole Framework so even if the underlying principles are the same it could be likely done quite differently in ASP.NET.

    If you were talking rather about authentication you have a whole part of ASP.NET to handle this for you with likely a quite steep learning curve but allowing to easily have this done to focus rarther on business needs once familiar with it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 11:13 AM
  • User2072242043 posted

    Thanks PatriceSc.
    I am very new here at asp.net and trying to learn things which comes in my experience on daily basis.
    as before i worked with php thats why i did show a similarity with asp.net in my question.
    IT means for redirecting i should only use response.redirect method than whatever i use in my conditions whether it is a session like

     if (Session["SessionName"] != null && Session["SessionRole"]=="Super Admin")
    	                    {
                                 Response.Redirect("~/superAdmin");    
                                	
                            }

    or simple IsPost?

    Is header is php function or a part of html?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 12:00 PM
  • User325035487 posted

    The code above should work. Instead of checking !=null in every page just write the null check in _PageStart.cshtml in root.

    if (Session["SessionName"] != null)
    - You Can then  restore the sesssion variable in the else part if it gets to be null

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2015 8:49 PM