Page 1 of 1

PHP Newbie Needs Help With Session Variable

Posted: Thu Jul 06, 2006 4:54 pm
by D@extra
Hi everyone,

I need a little help. I am not a PHP developer, but I can read code. I used to do ASP.

I will try ot explain what I need.

===========================================

I want to give my partner a link that carries the id at the end. For example

Code: Select all

http://www.domainname.com/index.php?ID=someid


OR some other form of a string that would pass the variable. This index page is currently HTML only, but I have no problem replacing it with a PHP document.

then I want to store the ID into a session variable.

And then I want to pass the session variable to the hidden form element

Code: Select all

<input type="hidden" name="myvar" value="someid">
===========================================

Can someone please explain how to do that.

I am ok passed the form and what i am doing with the form. I am storing the variables from the form already,,, I just need this one made to work somehow.

Thank you everyone in advance :)

Posted: Thu Jul 06, 2006 5:08 pm
by Christopher

Code: Select all

session_start();
$_SESSION['ID'] = preg_replace('/[^a-zA-Z0-9\-\_]/', '', $_POST['ID']);
// add any other characters you want to allow
// use $_GET if it is not from a form
And for the form:

Code: Select all

<input type="hidden" name="myvar" value="<?php echo $_SESSION['ID']; ?>">
And you may want to escape that output and convert html entities of you allow more characters above.

Posted: Thu Jul 06, 2006 5:22 pm
by D@extra
Thank you :)

Before I go off to implement this let me ask one more thing...

Code: Select all

session_start(); 
$_SESSION['ID'] = preg_replace('/[^a-zA-Z0-9\-\_]/', '', $_POST['ID']);
Can you explain in short, how does this code string "pick up" the variable from a link that looks like this.

Code: Select all

http://mydomain.com/index.php?ID=SOMEID
Does PHP use

Code: Select all

$_POST
to read the QUERY_STRING?

What I am doing is basicaly the same as the principle with affiliate links. Only I don't need affiliate tracking application. I need something far smaller.

Posted: Thu Jul 06, 2006 5:28 pm
by RobertGonzalez
You should probably have a read through of the sessions portion of the PHP manual. Specifically, you are going to be using the session_start() function, the $_SESSION superglobal and the echo language construct.

Start there, try something, then post back if needed.

Posted: Thu Jul 06, 2006 7:47 pm
by D@extra
Awesome!

Thanks for your help!

This was all I needed realy:

Code: Select all

$_SESSION['ID'] = preg_replace('/[^a-zA-Z0-9\-\_]/', '', $_GET['ID']);
I did all the rest correctly... but missed the right way to pass the querystring... :)

Thank you once again. You helped a million.