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 Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/ Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 Pass it in a session. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-580833 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? Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-580837 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. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-580839 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'; } ?> Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-581941 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 Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-581947 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... Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-581948 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] Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582077 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 ' '. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582086 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; Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582404 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"; ? Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582407 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 Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582530 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. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582603 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. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-582607 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. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-583560 Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 No problem. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-583578 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? Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-583937 Share on other sites More sharing options...
foochuck Posted July 8, 2008 Author Share Posted July 8, 2008 *bump* Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-584690 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"]; Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-584695 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... Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-584700 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. Link to comment https://forums.phpfreaks.com/topic/113078-passing-a-long-variable-through-web-pages/#findComment-584702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.