The code tries to write a cookie with the name ref and if is not able to write that cookie it will create a session var with the name ref. The value must be retrieved after that in a hidden form field.
Say you have a page "page1.aspx" . The codebehind of this page will look like
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty( Request["ref"]))
{
SetCookie(Request["ref"]);
}
else
{
SetCookie("");
}
}
public string GetCookieValue()
{
//Grab the cookie
HttpCookie cookie = Request.Cookies["ref"];
//Check to make sure the cookie exists
if (cookie == null)
{
return Session["ref"].ToString();
}
else
{
//Write the cookie value
return =cookie.Value.ToString();
}
}
private void SetCookie(string val)
{
//Create a new cookie, passing the name into the constructor
HttpCookie cookie = new HttpCookie("ref");
//Set the cookies value
cookie.Value = val;
cookie.Expires = DateTime.Now.AddMonths(1);
//Add the cookie
try {
Response.Cookies.Add(cookie);
}
catch {
Session["ref"] = Request["ref"];
}
}
}
NOTE: There will be slight mistake in the code, as I have written this on the fly without using any editor and a compiler :). So sorry for that..
asp.net cookie
I blog at http://rajeshpillai.net and have a community startup http://ownabook.org/
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
Thank you very much for your answer. It is highly appreciated. However if anyone can take a look at it and make a completion if there are errors or not it would be even better because, honestly, I can't tell anything about ASP .NET, even if I recognize things
I'm not to a level where I can have a authorized opinion. I use PHP and BASH Shell most of the time.
sihastru
Member
1 Points
2 Posts
Who can translate this? Small PHP to ASP .NET
Dec 11, 2009 09:36 AM|LINK
Hi,
I'm a PHP Developer but I want to help one of my friends who is a newbie. Can anyone from here translate this code from PHP to ASP .NET?
Part one: TO BE PLACED IN THE HEADER!
/* BEGIN */
if(isset($_REQUEST['ref']) && $_REQUEST['ref'] != ""){
$expire = time()+60*60*24*30; // 30 days
$do = setcookie('ref', $_REQUEST['ref'], $expire);
if( ! $do){
$_SESSION['ref'] = $_REQUEST['ref'];
}
}
else{
$expire = time()+60*60*24*30; // 30 days
$do = setcookie('ref', '', $expire);
if( ! $do){
$_SESSION['ref'] = $_REQUEST['ref'];
}
}
/* END */
Part two: TO BE PLACED IN THE FORM!
/* BEGIN */
<input type="hidden" name="ref" value="<?php echo isset($_COOKIE['ref']) ? $_COOKIE['ref'] : $_SESSION['ref']; ?>"/>
/* END */
The code tries to write a cookie with the name ref and if is not able to write that cookie it will create a session var with the name ref. The value must be retrieved after that in a hidden form field.
Thank you in advance.
Marius
ASP .net 2.0 PHP code translation
thinkrajesh
Participant
1356 Points
232 Posts
Re: Who can translate this? Small PHP to ASP .NET
Dec 11, 2009 10:13 AM|LINK
Say you have a page "page1.aspx" . The codebehind of this page will look like
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty( Request["ref"])) { SetCookie(Request["ref"]); } else { SetCookie(""); } } public string GetCookieValue() { //Grab the cookie HttpCookie cookie = Request.Cookies["ref"]; //Check to make sure the cookie exists if (cookie == null) { return Session["ref"].ToString(); } else { //Write the cookie value return =cookie.Value.ToString(); } } private void SetCookie(string val) { //Create a new cookie, passing the name into the constructor HttpCookie cookie = new HttpCookie("ref"); //Set the cookies value cookie.Value = val; cookie.Expires = DateTime.Now.AddMonths(1); //Add the cookie try { Response.Cookies.Add(cookie); } catch { Session["ref"] = Request["ref"]; } } }The view page1.aspx
NOTE: There will be slight mistake in the code, as I have written this on the fly without using any editor and a compiler :). So sorry for that..
asp.net cookie
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
sihastru
Member
1 Points
2 Posts
Re: Who can translate this? Small PHP to ASP .NET
Dec 11, 2009 10:56 AM|LINK
Hi,
Thank you very much for your answer. It is highly appreciated. However if anyone can take a look at it and make a completion if there are errors or not it would be even better because, honestly, I can't tell anything about ASP .NET, even if I recognize things I'm not to a level where I can have a authorized opinion. I use PHP and BASH Shell most of the time.
Thank you again very, very much.
Marius