foochuck Posted July 3, 2008 Share Posted July 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 Pass it in a session. Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 3, 2008 Author Share Posted July 3, 2008 I'll give that a try. Know of any good tutorials on PHP sessions? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 Just google it and you'll find some. This one's pretty good I think. Sessions are veryyy easy. Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 4, 2008 Author Share Posted July 4, 2008 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'; } ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 4, 2008 Share Posted July 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
taith Posted July 4, 2008 Share Posted July 4, 2008 <?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... Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 5, 2008 Author Share Posted July 5, 2008 <?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] Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 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 ' '. Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 5, 2008 Author Share Posted July 5, 2008 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; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 You can, but you shouldn't...and if you're going to do that, why don't you just do: $_SESSION['somevar'] = "abcdefghijklmnopqrstuvwxyz"; ? Quote Link to comment Share on other sites More sharing options...
piyushsharmajec Posted July 5, 2008 Share Posted July 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 6, 2008 Author Share Posted July 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 7, 2008 Author Share Posted July 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 No problem. Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 7, 2008 Author Share Posted July 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 8, 2008 Author Share Posted July 8, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 8, 2008 Share Posted July 8, 2008 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"]; Quote Link to comment Share on other sites More sharing options...
foochuck Posted July 8, 2008 Author Share Posted July 8, 2008 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... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 8, 2008 Share Posted July 8, 2008 Okay. And the reason is wasn't working before was because you have ' ' and not " ". =P It didn't evaluate the variable. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.