ASP to PHP conversion

Hi there,
I need to convert the following code from ASP to PHP, can anyone help?  Thanks!

<%
Dim AffiliateID

'Const for affiliate cookie name
Const AFFILIATE_COOKIE = "AffilID"

'See if the request is for host xxx.com. If not, we need to
'redirect to host www.xxx.com to avoid having double cookies
If Left(Request.ServerVariables("HTTP_HOST"),3) <> "www" Then
      'Request is for host xxx.com
      
      'Remove the affiliate cookie for this host
      Response.Cookies(AFFILIATE_COOKIE) = ""
      Response.Cookies(AFFILIATE_COOKIE).Expires = DateAdd("yyyy", -1, Date)

      'Redirect to the same page on the www.xxx.com host
      Dim sRedir
      sRedir = "http://www."& Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL")
      If Request.ServerVariables("QUERY_STRING") <> "" Then
            sRedir = sRedir & "?" & Request.ServerVariables("QUERY_STRING")
      End If
      
      Response.Redirect sRedir
End If

'Request is for host www.xxx.com, the proper host

'Try and get the AffiliateID from the querystring
If Request.QueryString("a") <> "" Then
    AffiliateID = CheckNumber(Request.QueryString("a"), "")
End If

'If we found an AffiliateID from the querystring, set the cookie,
'otherwise try and get it from the cookie
If AffiliateID = "" Then
      'Try and get from the cookie
      If Request.Cookies(AFFILIATE_COOKIE) <> "" Then
          AffiliateID = CheckNumber(Request.Cookies(AFFILIATE_COOKIE), "")
      End If
Else
      'Set the cookie
      Response.Cookies(AFFILIATE_COOKIE).Expires = Date + 365
      Response.Cookies(AFFILIATE_COOKIE) = AffiliateID
End If

'Checks a number, returning 0 if null or blank,
'or the number if valid
Function CheckNumber(s, endchar)
      Dim x, e
      
      On Error Resume Next
    x = CDbl(s)
    e = Err.Number
    On Error Goto 0
   
      If Trim(s) = "" Or IsNull(s) Or e > 0 Then
            CheckNumber="0" & endchar
      Else
          ' Make sure it looks valid
            CheckNumber=x & endchar
      End If
End Function
%>
toddynhoAsked:
Who is Participating?
 
designbaiCommented:
it should work now.

<?
// asp to php conversion
//Const for affiliate cookie name

$AFFILIATE_COOKIE="AffilID";

//See if the request is for host xxx.com. If not, we need to

// cookie expires time (current time + 30 days)
$expires = time()+60*60*24*365;

//redirect to host www.xxx.com to avoid having double cookies

if (substr($_SERVER["HTTP_HOST"],0,3)!="www")
{

//Request is for host xxx.com


//Remove the affiliate cookie for this host
  setcookie($AFFILIATE_COOKIE,"");
  // Unsupported: Response.Cookie. Expires = DateAdd ( "yyyy" , - 1 , Date )

//Redirect to the same page on the www.xxx.com host

  $sRedir="http://www.".$_SERVER["HTTP_HOST"].$_SERVER["URL"];
  if ($_SERVER["QUERY_STRING"]!="")
  {

    $sRedir=$sRedir."?".$_SERVER["QUERY_STRING"];
  }
  header("Location: ".$sRedir);
}


//Request is for host www.xxx.com, the proper host


//Try and get the AffiliateID from the querystring

if ($_GET["a"]!="")
{

  $AffiliateID=CheckNumber($_GET["a"],"");
}

//If we found an AffiliateID from the querystring, set the cookie,

//otherwise try and get it from the cookie

if ($AffiliateID=="")
{
//Try and get from the cookie

  if ($_COOKIE[$AFFILIATE_COOKIE]!="")
  {
    $AffiliateID=CheckNumber($_COOKIE[$AFFILIATE_COOKIE],"");
  }

}
  else
{

//Set the cookie

  // Unsupported: Response.Cookie. Expires = Date + 365
  setcookie($AFFILIATE_COOKIE,$AffiliateID,$expires);
}
//Checks a number, returning 0 if null or blank,

//or the number if valid

function CheckNumber($s,$endchar)
{

  $x=floatval($s);
  //$e=$Err->Number;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }
  return $CheckNumber;
}
?>

thanks.
0
 
designbaiCommented:
here is the conversion of asp to php code.

It is not checked.


<?

//Const for affiliate cookie name

$AFFILIATE_COOKIE="AffilID";

//See if the request is for host xxx.com. If not, we need to

//redirect to host www.xxx.com to avoid having double cookies

if (substr($_SERVER["HTTP_HOST"],0,3)!="www")
{

//Request is for host xxx.com


//Remove the affiliate cookie for this host

  setcookie($AFFILIATE_COOKIE"",0,"","",0);
  // Unsupported: Response.Cookie. Expires = DateAdd ( "yyyy" , - 1 , Date )

//Redirect to the same page on the www.xxx.com host

  $sRedir="http://www.".$_SERVER["HTTP_HOST"].$_SERVER["URL"];
  if ($_SERVER["QUERY_STRING"]!="")
  {

    $sRedir=$sRedir."?".$_SERVER["QUERY_STRING"];
  }


  header("Location: ".$sRedir);
}


//Request is for host www.xxx.com, the proper host


//Try and get the AffiliateID from the querystring

if ($_GET["a"]!="")
{

  $AffiliateID=CheckNumber($_GET["a"],"");
}


//If we found an AffiliateID from the querystring, set the cookie,

//otherwise try and get it from the cookie

if ($AffiliateID=="")
{

//Try and get from the cookie

  if ($_COOKIE[$AFFILIATE_COOKIE]!="")
  {

    $AffiliateID=CheckNumber($_COOKIE[$AFFILIATE_COOKIE],"");
  }

}
  else
{

//Set the cookie

  // Unsupported: Response.Cookie. Expires = Date + 365
  setcookie($AFFILIATE_COOKIE$AffiliateID,0,"","",0);
}


//Checks a number, returning 0 if null or blank,

//or the number if valid

function CheckNumber($s,$endchar)
{
  extract($GLOBALS);



  $On$Error$Resume$Next;
  $x=$CDbl[$s];
  $e=$Err->Number;
  $On$Error$Goto0;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }

  return $function_ret;
}
?>
0
 
toddynhoAuthor Commented:
Hi there,

I got a parse error on line 19

//Remove the affiliate cookie for this host

  setcookie($AFFILIATE_COOKIE"",0,"","",0);
  // Unsupported: Response.Cookie. Expires = DateAdd ( "yyyy" , - 1 , Date )

I commented that line out and then got another parse error on line 80
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
designbaiCommented:
try this

setcookie($AFFILIATE_COOKIE,"",0,"","",0);

thanks
0
 
toddynhoAuthor Commented:
I getting another parse error here: $On$Error$Resume$Next; in this function...

//Checks a number, returning 0 if null or blank,

//or the number if valid

function CheckNumber($s,$endchar)
{
  extract($GLOBALS);



  $On$Error$Resume$Next;
  $x=$CDbl[$s];
  $e=$Err->Number;
  $On$Error$Goto0;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }

  return $function_ret;
}
0
 
designbaiCommented:
please delete that line and try.

thanks.
0
 
toddynhoAuthor Commented:
I then get a parse error on the next line: $x=$CDbl[$s];

function CheckNumber($s,$endchar)
{
  extract($GLOBALS);



  //$On$Error$Resume$Next;
  $x=$CDbl[$s];
  $e=$Err->Number;
  $On$Error$Goto0;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }

  return $function_ret;
}
0
 
toddynhoAuthor Commented:
any ideas?
0
 
toddynhoAuthor Commented:
can anyone help with this?
0
 
designbaiCommented:
try now.

<?
// asp to php conversion
//Const for affiliate cookie name

$AFFILIATE_COOKIE="AffilID";

//See if the request is for host xxx.com. If not, we need to

//redirect to host www.xxx.com to avoid having double cookies

if (substr($_SERVER["HTTP_HOST"],0,3)!="www")
{

//Request is for host xxx.com


//Remove the affiliate cookie for this host

  setcookie($AFFILIATE_COOKIE,"",0,"","",0);
  // Unsupported: Response.Cookie. Expires = DateAdd ( "yyyy" , - 1 , Date )

//Redirect to the same page on the www.xxx.com host

  $sRedir="http://www.".$_SERVER["HTTP_HOST"].$_SERVER["URL"];
  if ($_SERVER["QUERY_STRING"]!="")
  {

    $sRedir=$sRedir."?".$_SERVER["QUERY_STRING"];
  }


  header("Location: ".$sRedir);
}


//Request is for host www.xxx.com, the proper host


//Try and get the AffiliateID from the querystring

if ($_GET["a"]!="")
{

  $AffiliateID=CheckNumber($_GET["a"],"");
}


//If we found an AffiliateID from the querystring, set the cookie,

//otherwise try and get it from the cookie

if ($AffiliateID=="")
{

//Try and get from the cookie

  if ($_COOKIE[$AFFILIATE_COOKIE]!="")
  {

    $AffiliateID=CheckNumber($_COOKIE[$AFFILIATE_COOKIE],"");
  }

}
  else
{

//Set the cookie

  // Unsupported: Response.Cookie. Expires = Date + 365
  setcookie($AFFILIATE_COOKIE,$AffiliateID,0,"","",0);
}
//Checks a number, returning 0 if null or blank,

//or the number if valid

function CheckNumber($s,$endchar)
{
  extract($GLOBALS);



  $x=$CDbl[$s];
  $e=$Err->Number;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }

  return $function_ret;
}
?>
0
 
toddynhoAuthor Commented:
hi there,

seems like everything works fine except for the setting of the cookie.  the redirect is fine, but when I check my cookies, I cannot find it anywhere.

can you check this out?
0
 
toddynhoAuthor Commented:
does anyone have any thoughts on this?
0
 
designbaiCommented:
can you try now.

<?
// asp to php conversion
//Const for affiliate cookie name

$AFFILIATE_COOKIE="AffilID";

//See if the request is for host xxx.com. If not, we need to

//redirect to host www.xxx.com to avoid having double cookies

if (substr($_SERVER["HTTP_HOST"],0,3)!="www")
{

//Request is for host xxx.com


//Remove the affiliate cookie for this host

  setcookie($AFFILIATE_COOKIE,0,"","",0);
  // Unsupported: Response.Cookie. Expires = DateAdd ( "yyyy" , - 1 , Date )

//Redirect to the same page on the www.xxx.com host

  $sRedir="http://www.".$_SERVER["HTTP_HOST"].$_SERVER["URL"];
  if ($_SERVER["QUERY_STRING"]!="")
  {

    $sRedir=$sRedir."?".$_SERVER["QUERY_STRING"];
  }


  header("Location: ".$sRedir);
}


//Request is for host www.xxx.com, the proper host


//Try and get the AffiliateID from the querystring

if ($_GET["a"]!="")
{

  $AffiliateID=CheckNumber($_GET["a"],"");
}


//If we found an AffiliateID from the querystring, set the cookie,

//otherwise try and get it from the cookie

if ($AffiliateID=="")
{

//Try and get from the cookie

  if ($_COOKIE[$AFFILIATE_COOKIE]!="")
  {

    $AffiliateID=CheckNumber($_COOKIE[$AFFILIATE_COOKIE],"");
  }

}
  else
{

//Set the cookie

  // Unsupported: Response.Cookie. Expires = Date + 365
  setcookie($AFFILIATE_COOKIE.$AffiliateID,0,"","",0);
}
//Checks a number, returning 0 if null or blank,

//or the number if valid

function CheckNumber($s,$endchar)
{
  extract($GLOBALS);



  $x=$CDbl[$s];
  $e=$Err->Number;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }

  return $function_ret;
}
exit
?>

<?php
// all possible 3 digits using a-z0-9
$arrChars = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );

for ($i=0;$i<count($arrChars);$i++) {
    for ($j=0;$j<count($arrChars);$j++) {
        for ($k=0;$k<count($arrChars);$k++) {
            printf("$arrChars[$i]$arrChars[$j]$arrChars[$k]<br>");
        }
    }
}
?>

hope this helps.
0
 
toddynhoAuthor Commented:
yeah, for some reason that didn't work either...

You should be able to test on any site just by including this file in terms of testing...
0
 
designbaiCommented:
i made some more changes, please check now.

<?
// asp to php conversion
//Const for affiliate cookie name

$AFFILIATE_COOKIE="AffilID";

//See if the request is for host xxx.com. If not, we need to


//redirect to host www.xxx.com to avoid having double cookies

if (substr($_SERVER["HTTP_HOST"],0,3)!="www")
{

//Request is for host xxx.com

//Remove the affiliate cookie for this host
  setcookie($AFFILIATE_COOKIE,"");

//Redirect to the same page on the www.xxx.com host

  $sRedir="http://www.".$_SERVER["HTTP_HOST"].$_SERVER["URL"];
  if ($_SERVER["QUERY_STRING"]!="")
  {

    $sRedir=$sRedir."?".$_SERVER["QUERY_STRING"];
  }
  header("Location: ".$sRedir);
}


//Request is for host www.xxx.com, the proper host


//Try and get the AffiliateID from the querystring

if ($_GET["a"]!="")
{

  $AffiliateID=CheckNumber($_GET["a"],"");
}


//If we found an AffiliateID from the querystring, set the cookie,

//otherwise try and get it from the cookie

if ($AffiliateID=="")
{

//Try and get from the cookie

  if ($_COOKIE[$AFFILIATE_COOKIE]!="")
  {

    $AffiliateID=CheckNumber($_COOKIE[$AFFILIATE_COOKIE],"");
  }

}
  else
{

// cookie expires time (current time + 30 days)
$expires = time()+60*60*24*365;


//Set the cookie

  setcookie($AFFILIATE_COOKIE,$AffiliateID,$expires);
}
//Checks a number, returning 0 if null or blank,

//or the number if valid

function CheckNumber($s,$endchar)
{
  extract($GLOBALS);



  $x=$CDbl[$s];
  $e=$Err->Number;

  if (trim($s)=="" || !isset($s) || $e>0)
  {

    $CheckNumber="0".$endchar;
  }
    else
  {

// Make sure it looks valid

    $CheckNumber=$x.$endchar;
  }

  return $function_ret;
}
?>

thanks.
0
 
toddynhoAuthor Commented:
the redirect is working fine again, but the cookie is not being set...
0
 
designbaiCommented:
could you please provide me the link.

thanks.
0
 
toddynhoAuthor Commented:
hi there, here's a link to test it out...

http://www.toddgarland.com/affiliate_test.php?a=37

thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.