{"id":71,"date":"2006-04-21T12:16:45","date_gmt":"2006-04-21T11:16:45","guid":{"rendered":"http:\/\/www.galhano.com\/blog\/?p=71"},"modified":"2008-02-07T18:38:38","modified_gmt":"2008-02-07T17:38:38","slug":"purpose-of-session-variables","status":"publish","type":"post","link":"https:\/\/galhano.com\/blog\/?p=71","title":{"rendered":"Purpose of Session variables"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" width=\"162\" height=\"50\" align=\"left\" alt=\"logo asp.net\" id=\"image55\" src=\"http:\/\/www.galhano.com\/blog\/wp-content\/2006\/04\/aspnet32.gif\" \/><br \/>\n<font size=\"2\" face=\"Verdana\">  <\/font><\/p>\n<table width=\"195\" align=\"right\" id=\"table1\" style=\"height: 44px\">\n<tr>\n<td><font size=\"2\" face=\"Verdana\"> <!-- 336x280 Rectangle Ad Tag Start --> <!-- 336x280 Rectangle Ad Tag End -->  \t\t<\/font><\/td>\n<\/tr>\n<\/table>\n<div align=\"left\"><font size=\"2\" face=\"Verdana\">In most of the web sites, when a user log In in  the login page, they set few variables to session.<\/font><\/div>\n<p><font size=\"2\" face=\"Verdana\">  <\/font><\/p>\n<table width=\"100%\" border=\"1\" id=\"table4\">\n<tr>\n<td bgcolor=\"#f2f2f2\"><code>&lt;font size=&quot;2&quot; face=&quot;Verdana&quot;&gt;if (bLoginSuccess  \t\t= true) then<br \/>\nSession(&quot;UserId&quot;) = txtLoginName.Text<br \/>\nSession(&quot;Name&quot;) = GetUserNameFromDatabase(txtLoginName.Text)<br \/>\nelse<br \/>\nResponse.Redirect (&quot;LoginError.aspx&quot;)<br \/>\nend if&lt;\/font&gt;<\/code><\/td>\n<\/tr>\n<\/table>\n<p><font size=\"2\" face=\"Verdana\">T<\/font><font size=\"2\" face=\"Verdana\">he above code stores the user&#8217;s userid into a  session variable called &#8220;UserId&#8221;. All other pages will check if the user id is  set in the session and if not, it will give an error message saying &#8220;you have  not logged in&#8221;.<br \/>\n<\/font><\/p>\n<table width=\"100%\" border=\"1\" id=\"table3\">\n<tr>\n<td bgcolor=\"#f2f2f2\"><code>&lt;font size=&quot;2&quot; face=&quot;Verdana&quot;&gt;dim userId as  \t\tstring = Session(&quot;UserId&quot;)<br \/>\nif ( userId = &quot;&quot; ) then<br \/>\nResponse.Redirect(&quot;Login.aspx&quot;)<br \/>\nelse<br \/>\nResponse.Write (&quot;Welcome &quot; &amp; Session(&quot;Name&quot;))<br \/>\nend if&lt;\/font&gt;<\/code><\/td>\n<\/tr>\n<\/table>\n<p><font size=\"2\" face=\"Verdana\"><br \/>\nThe above validation will be required only in the pages which needs user login.  For example, in AspSpider.com, you can access m<\/font><font size=\"2\" face=\"Verdana\">ost of the pages without logging  in. Only when you submit an article or feedback, we will use the above code and  validate your login. If you are not logged in, we will redirect you to login  page automatically.<\/p>\n<p>If you look at the top left corner of this site, you can either see &#8216;Login&#8221; or &#8220;Welcome  &#8220;. We have used a logic similar to what is shown below, to display  appropriate message.<br \/>\n<\/font><\/p>\n<table width=\"100%\" border=\"1\" id=\"table2\">\n<tr>\n<td bgcolor=\"#f2f2f2\"><code>&lt;font size=&quot;2&quot; face=&quot;Verdana&quot;&gt;dim userId as  \t\tstring = Session(&quot;UserId&quot;)<br \/>\nif ( userId = &quot;&quot; ) then<br \/>\nResponse.Write(&quot;&lt;a href=&quot;http:\/\/www.galhano.com\/blog\/Login.aspx&quot;&gt;Login&lt;\/a&gt;&quot;)<br \/>\nelse<br \/>\nResponse.Write (&quot;Welcome &lt;a href=&quot;http:\/\/www.galhano.com\/blog\/MyAccount.aspx&quot;&gt;&quot; &amp; Session(&quot;Name&quot;) &amp;  \t\t&quot;&lt;\/a&gt;)<br \/>\nend if&lt;\/font&gt;<\/code><\/td>\n<\/tr>\n<\/table>\n<p><font size=\"2\" face=\"Verdana\">As you can see from the above examples, session  variables are used to store small key-value pairs in the memory. You can use  session variables to store values from one page and access the values from other  pages for the same user. If you set a value in session variable from one page,  you can retrieve the value from any other page in the same session.<\/p>\n<p>The most important point to remember is, whatever value you store in session  will be valid only until the session expires. Also, this value will not be  accessible for another user\/session.<\/font><\/p>\n<p><font size=\"2\" face=\"Verdana\"><br \/>\n<\/font><!--a9bf239744bb81ff256346ae571a35ca--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In most of the web sites, when a user log In in the login page, they set few variables to session. &lt;font size=&quot;2&quot; face=&quot;Verdana&quot;&gt;if (bLoginSuccess = true) then Session(&quot;UserId&quot;) = txtLoginName.Text Session(&quot;Name&quot;) = GetUserNameFromDatabase(txtLoginName.Text) else Response.Redirect (&quot;LoginError.aspx&quot;) end if&lt;\/font&gt; The above code stores the user&#8217;s userid into a session variable called &#8220;UserId&#8221;. All other pages [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-aspnet","author-admin"],"_links":{"self":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":0,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}