PHP Newbie Needs Help With Session Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
D@extra
Forum Newbie
Posts: 3
Joined: Thu Jul 06, 2006 4:42 pm

PHP Newbie Needs Help With Session Variable

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
D@extra
Forum Newbie
Posts: 3
Joined: Thu Jul 06, 2006 4:42 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
D@extra
Forum Newbie
Posts: 3
Joined: Thu Jul 06, 2006 4:42 pm

Post 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.
Post Reply