Hello,
I am developing a site with Dreamweaver CS3 and have a little knowledge of HTML.
I need a solution for the following:
Visitors can come to my site through a referrer for ex: www.domain.com/?ref=cesar017
The referrer’s information (cesar017) should be prefilled automatically in a form field on another page than the index page. This means the visitor first can look around on the other pages of the site and then go to the page with the form. I don’t use a database at this moment, I only want a certain field automatically prefilled with the referrer’s info from the URL for ex : <input type= hidden -or text- name= partner id= partner value= ""/> In this case the field should have the value=”cesar017”
I understood this can be solved with Javascript or PHP and cookies, but I nearly don’t know anything about this and Dreamweaver has not automated this function.
Can somebody help me with this script (how, where * head - body section *…)
In advance thanks for your help

Dani AI

Generated

Good concise approach from — using a persistent client-side value (cookie) is the simplest way to carry a URL parameter across pages — and good caution from about validation. The thread left two gaps worth closing: (1) a safer, server-side way to persist the ref while avoiding easy tampering, and (2) practical validation/escaping rules before inserting the value into a form.

A robust server-side option is to store the incoming ref in the PHP session (server-held data) instead of trusting a cookie value directly. On the landing page start the session and save a sanitized ref; on the form page read it and escape it before output. Example pattern (place at the very top of PHP files, before any HTML output):

<?php
session_start();
if (!empty($_GET['ref'])) {
  // allow only letters, numbers, dash and underscore
  $ref = preg_replace('/[^A-Za-z0-9_-]/', '', $_GET['ref']);
  $_SESSION['partner'] = $ref;
}
?>

Then echo the session value into the form using proper HTML escaping (e.g. htmlspecialchars with ENT_QUOTES, UTF-8). This reduces client-side tampering compared with storing the raw value in a cookie.

Security and reliability notes: always validate the ref against a whitelist or a server-side map of known partner codes (don’t accept arbitrary strings). If you need tamper-proofing for production links, use a server-generated signature (HMAC with a server secret, e.g. hash_hmac('sha256', ...)) and verify the signature before accepting the ref. If you stick with cookies, set Secure and HttpOnly flags and pick a reasonable expiry. Finally, beware of PHP output/headers: start sessions or send cookies before any output (no stray whitespace or BOM), or use output buffering.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

You've come through to the php forum, so if you want a js solution, post it under the Javascript/DHTML/Ajax forum.

Else, php can do this in a couple of ways. You can propagate variables either through a DB, session variables or cookies.

The cookie method may be the most painless for newbies. You simply create a cookie value based on the url querystring.
The form then searches for the cookie and inserts the value a field.

HOWEVER
Using a simple querystring for input data has its dangers. Anybody can change the querystring to anything they want, so you should have some validation / cleaning and verification before the value is accepted and placed into the form.

A php variable that could be used is $_SERVER to tell you which site sent the user to your index page. This can also be spoofed, so again, you'd need some verification/validation.

A confirm code could / should be added to querystrings, e.g.

THis can be created via the md5() function and should contain a 'salt' as well as a string that can be checked against the id value.

Hello,

First of all my apologies for the delay in my replay (I have been unable to post these last months for personal reasons. Thanks for understanding).
This is the solution that is working and is the best one for my needs :

Add this anywhere before your HTML begins to start the cookie.
PHP Code:

<?php
if(isset($_GET['ref']))
  setcookie('referrer', $_GET['ref'], time()+3600);
?>

cookie will remain on client-side for 1 hour. If you want to store the cookie for a longer period on client-side
time()+60*60*24*30 is seconds*minits*hours*days
if time is O or not mentioned, than the cookie will be canceled at the end of the session

Then use the cookie on your page with the form field :

<input type="text" name="referrer" value="<?php
  echo htmlspecialchars($_COOKIE['referrer']); ?>" />

Thanks to all the people that helped me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.