Forum Moderators: coopster

Message Too Old, No Replies

Inserting and storing parameter from URL

         

mrgrier74

9:47 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



I'm new to PHP and really could use some assistance with what is to most an easy problem.

I have a URL like: www.mysite.com?_repid=12345

I want to insert the repid parameter into a form field named RepID and also create a cookie so if a user comes back, the RepID is called. (I'm probably using the wrong terminology)

I know how to use the GET function to pass the URL parameter to the field form, but don't know where to go from here?

mrgrier74

9:48 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



BTW, thank you in advance for any suggestions or help!

rocknbil

7:38 pm on Feb 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cleanse the variable, make sure it's a number.

if (isset($_GET['_repid']) and ($_GET['_repid'] > 0)) {
$myvar = $_GET['_repid'];
}

To put it in a form, you can use single quotes and concatenate,

$frm = '
<form method="post" action="somescript.php">
<input type="text" value="' . $myvar . '">
</form>
';
echo $frm;

Or double quotes and escape,

$frm = "
<form method=\"post\" action=\"somescript.php\">
<input type=\"text\" value=\"$myvar\">
</form>
";
echo $frm;

Or use double quotes but use single quotes on the form attributes, which I won't show you. There are various reasons not to use single quotes in form attributes, but since it "looks the same" and is "easier" you'll see it everywhere.

So now you have a value var, you want to set a cookie. Choose a name for the cookie, make it something identifiable so if anyone is snooping they won't suspect you of doing something sneaky (just my preference there:)


$cookiename = 'SITENAME_AFFILIATE';
set_aff_cookie($cookiename,$myvar,1800);
//
function set_aff_cookie($cname,$cvalue,$expires_in) {
$cookietime = (isset($expires_in))?$expires_in:3600;
$cookiename = (isset($cname))?$cname:'SITENAME_DEFAULT';
setcookie ($cookiename, $cvalue, time()+$cookietime, '/');
}


Cookie expire time is in milliseconds, 3600 is one hour.

Begging the next question, you'll want to read it:


$my_cookie_val = read_cookie($cookiename);
//
function read_cookie($cname) {
$this_cookie=NULL;
$cookiename = (isset($cname))?$cname:'SITENAME_DEFAULT';
if (isset($_COOKIE[$cookiename])) { $this_cookie = $_COOKIE[$cookiename]; }
return $this_cookie;
}


You may think there's not enough being done here to move these into a function, but as your site grows you will have a need for setting and reading more and different cookies. You'd just add them as you need them, for returning values from read_cookie, you return an array.

mrgrier74

6:37 pm on Feb 8, 2010 (gmt 0)

10+ Year Member



Thanks for the help. I'm still having a little trouble, but its form lack of sitting down and trying harder. I will keep you posted.