Dynamically creating $_SESSION variables

Reply

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Dynamically creating $_SESSION variables

 
0
  #1
Feb 6th, 2008
Hi there

I'm having a bit of a problem. As the title suggests, I'm creating $_SESSION's such as
  1. $_SESSION[''.$row[0].'']
dynamically which is working perfectly, but the problem is I don't know how to call that same variable on a template page that I click through to.

At the moment I have the following:
  1. $query = "SELECT eid, title, content, image, image2 FROM diary WHERE eid = '".$_SESSION[''.$row[0].'']."'";
Clearly this isn't working because the session variable could be any of the id's pulled from the database on the previous page and therefore the template page shows no content. I need some way of tying the variable from the previous page to the variable on the template page without using symantic URL encoding.
Last edited by Venom Rush; Feb 6th, 2008 at 7:14 am.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,744
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Dynamically creating $_SESSION variables

 
0
  #2
Feb 6th, 2008
Why dont you assign the value to a variable and then use that variable ?
Eg.
  1. <?php //page1.php
  2. session_start();
  3. $_SESSION['eid']="1234";
  4. // rest of your code...
  5. ?>
  6. <?php //page2.php
  7. session_start();
  8. $eid=$_SESSION['eid'];
  9. $query = "SELECT eid, title, content, image, image2 FROM diary WHERE eid ='$eid'";
  10. // rest of your code
  11. ?>
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Dynamically creating $_SESSION variables

 
0
  #3
Feb 6th, 2008
Originally Posted by nav33n View Post
Why dont you assign the value to a variable and then use that variable ?
Eg.
  1. <?php //page1.php
  2. session_start();
  3. $_SESSION['eid']="1234";
  4. // rest of your code...
  5. ?>
  6. <?php //page2.php
  7. session_start();
  8. $eid=$_SESSION['eid'];
  9. $query = "SELECT eid, title, content, image, image2 FROM diary WHERE eid ='$eid'";
  10. // rest of your code
  11. ?>
Beacuse my code goes like this:

  1. <?php //page1.php
  2. session_start();
  3. // some code
  4. //create query
  5. $query = "SELECT eid, title, SUBSTRING(content, 1, 200), thumb_image FROM diary ORDER BY eid DESC";
  6. // some more code
  7. while($row = mysql_fetch_row($result))
  8. {
  9. if (!$_SESSION[$row[0]]) {
  10. $_SESSION[$row[0]] = $row[0];
  11. }
  12. //echo output here
  13. }
  14. // rest of your code...
  15. ?>
This is intended so that if I click on the link to the template page only the info relating to a particular eid will be displayed
Last edited by Venom Rush; Feb 6th, 2008 at 7:35 am.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,744
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Dynamically creating $_SESSION variables

 
0
  #4
Feb 6th, 2008
How are you differentiating between different links ? Say you have 4 links ? Arent you passing some id for different links ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Dynamically creating $_SESSION variables

 
0
  #5
Feb 6th, 2008
Originally Posted by nav33n View Post
How are you differentiating between different links ? Say you have 4 links ? Arent you passing some id for different links ?
No, at the moment it just links to template.php. What I'm trying to do is link through to the template page without having a URL that looks like the following:

http://www.sitename.com/template.php?eid=38

All I want people to see is:

http://www.sitename.com/template.php
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,744
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Dynamically creating $_SESSION variables

 
0
  #6
Feb 6th, 2008
And how will you know which $_SESSION[$row[0]] value has to be passed ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Dynamically creating $_SESSION variables

 
0
  #7
Feb 6th, 2008
Originally Posted by nav33n View Post
And how will you know which $_SESSION[$row[0]] value has to be passed ?
Thats exactly what I'm trying to figure out. Is there no way of doing this without having the info attached to the URL?
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,744
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Dynamically creating $_SESSION variables

 
0
  #8
Feb 6th, 2008
I dont think so. hmmm.. There is a weird way of doing it (which I wouldn't prefer). I hope you are looping to print the urls. What you can do is, use onclick event, set $_SESSION[$row[0]] value to a hidden variable and submit the form.
  1. <html>
  2. <body>
  3. <?php
  4. //connection
  5. //select db
  6. $query="select eid from table";
  7. $result=mysql_query($query);
  8. while($row=mysql_fetch_array($result)){
  9. echo "<form method=\"post\">";
  10. echo "<input type=\"hidden\" name=\"eid\">";
  11.  
  12. echo "<a href=\"#\" onclick=\"javascript: document.form.eid.value='$_SESSION[$row[0]]'; document.form.action=\"template.php\"; document.form.submit();\">";
  13. }
  14. ?>

As I said, this is the 'worst' way of doing it . But if you dont want to attach the info to the url, then you can do it this way ! I haven't tested the code(maybe the code wont even work!). Just giving you an idea on how it might work.
Cheers,
Naveen
Last edited by nav33n; Feb 6th, 2008 at 8:04 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Dynamically creating $_SESSION variables

 
0
  #9
Feb 6th, 2008
Hmmm, not ideal as you've said. Is there any real chance that a semantic URL attack could happen if I put the eid in the URL? Or am I relatively safe?
Last edited by Venom Rush; Feb 6th, 2008 at 8:24 am.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,744
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Dynamically creating $_SESSION variables

 
0
  #10
Feb 6th, 2008
You can have an array of all the eids and put it in a session. Then, everytime a user clicks on a link, check whether the eid is in the array of eids. If its present, then display respective page. Else, send the user to "you know where" ! Also, use mysql_real_escape_string or addslashes.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC