Jump to content

Passing a Long Variable through Web Pages


foochuck

Recommended Posts

I have a very long variable (over 250 characters) that I don't want to pass from one page to another. I don't want this variable shown in the URL either.

 

I'm guessing I'll have to use $_POST but I'm not sure how to pass this variable from one page to another without using it in a URL.

 

Could someone show me a sample of how to do this. Let's just say my variable is:

 

$alphabet="abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890";

 

FOO

Link to comment
Share on other sites

DarkWater,

 

Using the sample below from the tutorial...could you show me an example of how to set my very long variable and call it using Sessions?

 

<?php
session_start();

$visits = $_SESSION['visits']++;

if ($visits == 0) {
echo 'I have never seen you before, but I am glad you are here ';
}
else {
echo 'Welcome back! You have been here ', $visits, ' time(s) before';
}
?>

Link to comment
Share on other sites

Firstly, you'll need session_start() on every page that requires session variables, and it needs to be before ANY output (even whitespace), so I recommend putting it on top of your script.

 

So do:

 

page1.php:

session_start();

$_SESSION['somevar'] = $yourLongVariable;

 

page2.php:

session_start();

echo $_SESSION['somevar'];

 

I think you can figure it out from there. =P

Link to comment
Share on other sites

<?php
session_start();
if(!isset($_SESSION[var])) $_SESSION[var]='abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890';
?>

 

then on any page you want to access it... just call $_SESSION[var] as you would any other variable...

Link to comment
Share on other sites

<?php
session_start();
if(!isset($_SESSION[var])) $_SESSION[var]='abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890';
?>

 

then on any page you want to access it... just call $_SESSION[var] as you would any other variable...

 

You left out ' ' around var...do I need those or not?

 

$_SESSION[var]

Link to comment
Share on other sites

It's like any array.  You don't NEED them, but you SHOULD HAVE them, because it's neater code, it's guaranteed to work later on, and it is faster.

 

It works because PHP first looks for a constant named var because there are no quotation marks (which if it finds, you're screwed).  If it doesn't find it, it assumes you wanted a string so it casts it for you.  But you shouldn't rely on that.  Use ' '.

Link to comment
Share on other sites

It's like any array.  You don't NEED them, but you SHOULD HAVE them, because it's neater code, it's guaranteed to work later on, and it is faster.

 

It works because PHP first looks for a constant named var because there are no quotation marks (which if it finds, you're screwed).  If it doesn't find it, it assumes you wanted a string so it casts it for you.  But you shouldn't rely on that.  Use ' '.

 

Thanks DarkWater. One final question about your example for page 1:

 

session_start();
$_SESSION['somevar'] = $yourLongVariable;

 

Can I set $yourLongVariable on the same line where I'm copying it into $_SESSION['somevar'] ?

 

Like this...

 

session_start();
$_SESSION['somevar'] = $yourLongVariable = "abcdefghijklmnopqrstuvwxyz";

 

or should I do this...

 

session_start();
$yourLongVariable = "abcdefghijklmnopqrstuvwxyz";
$_SESSION['somevar'] = $yourLongVariable;

Link to comment
Share on other sites

you can also use  post variable $_POST['varname']='longvalue'; same like get variable

 

it cant sow in url and it holds value in page, where form is redirected by action.

 

Better learn by tutorials

 

I'm not really sure what the advantage of POST over SESSIONS would be...or vice versa.

Link to comment
Share on other sites

POST is for when sending in forms.  Trust me, you'll use sessions on this one.  If you use POST or GET, it can be modified before sending it to the page with very little effort from someone who knows what they're doing.

 

Thanks for all your help on this one DarkWater.

Link to comment
Share on other sites

DarkWater,

 

One more question - is there any way to interpolate a variable name into my session variable?

 

For instance, I'm creating several SESSION variables.

 

They are named:

 

$_SESSION['$aInfo'] = "This is only a test";

$_SESSION['$bInfo'] = "Another Test";

$_SESSION['$cInfo'] = "Yet another";

 

From there I'm passing a variable from one page to another...

 

I pass $folder

 

$folder = "a";

 

On the next page I want to interpolate the $folder variable into my SESSION variable name somehow like so:

 

$_SESSION['$folderInfo'];

 

That doesn't work and neither does...

 

$_SESSION[' . $folder . Info'];

 

or

 

$_SESSION[' . "$folder" . Info'];

 

I'm probably using the incorrect syntax. I have tested this on my page and I can verify that the session is working correctly

 

echo $_SESSION['aInfo']; // reads "This is only a test"

 

But I want to put $folder in place of a since I'm passing other variables through a URL.

 

Can you show me the correct syntax for passing $folder into the SESSION variable name?

Link to comment
Share on other sites

Remove the $ from these:

$_SESSION['$aInfo'] = "This is only a test";

$_SESSION['$bInfo'] = "Another Test";

$_SESSION['$cInfo'] = "Yet another";

 

And then do:

$_SESSION["{$folder}Info"];

 

Thanks...That's what I was looking for...FYI I mistyped and didn't have $ in those variable names...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.