Jeff
Jeff

Reputation: 1

How do I use the page URL as a form variable?

I'm trying to use the page URL as a variable inside a form submission so that I can write it into my database and figure out what pages are getting more list sign-ups. Is there any easy way to do this?

Upvotes: 0

Views: 97

Answers (5)

shmeeps
shmeeps

Reputation: 7853

Ideally, if every form links to the same parsing page, you have two options

Option 1: Created a hidden form variable on EVERY form page that records the URL of the page via static input, or from Javascript or PHP, both of which are detailed in the earlier answers.

Option 2: On the parsing page, use $url = $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"] to get the page that directed to your parsing page, and record that in the database.

I would probably go with Option 2, as it is a bit more elegant and easier to modify, and can even let you know if a user attempted to inject the form with a copy they stored locally on their hard drive.

Upvotes: 0

burntblark
burntblark

Reputation: 1700

$url = $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];

Upvotes: 2

xPheRe
xPheRe

Reputation: 2343

Put the value of $_SERVER["REQUEST_URI"] inside a hidden input in the origin form, so you can check for it on the form processing script.

Upvotes: 2

Lachezar
Lachezar

Reputation: 6723

<input type="hidden" name="page_url" id="page_url"/>
<script type="text/javascript">
document.getElementById("page_url").value = location.href;
</script>

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

$_SERVER["REQUEST_URI"]

Upvotes: 2

Related Questions