SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Using session variable instead of GET method

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using session variable instead of GET method

    Hi all.

    I am just practicing and learner to php. I am creating a page (following a youtube tutorial) to create and show users. Now i am adding links to modify and delete users (also show on that tutorial). This is my index.php file

    <html>

    <body>


    <?php session_start();
    include 'connection.php' ;
    $query = 'select * from people' ;

    $result = mysql_query($query);

    while ($person = mysql_fetch_array($result)) {
    echo $person['name'] ;
    echo "<br />" ;
    echo $person['description'] . "<br />";

    echo "<a href=\"modify.php?id=" . $person['id'] . "\">Modify User</a>" ;
    echo "<span> </span>" ;
    echo "<a href=\"delete.php?id=" . $person['id'] . "\">Delete User</a>" ;

    echo "<br />" ;
    }

    ?>

    <h1>Create A User </h1>
    <form action = "create.php" method = "post" >
    Nameinput type = "text" name = "inputname" value="" />
    Descriptioninput type = "text" name = "inputdesc" value="" />
    <br />
    <input type = "submit" name = "submit" />
    </form>

    </body>
    </html>

    Kindly see the bolded lines. If i dont want to use get method to pass the id values, how can i use session variable to do it ?

    kindly guide me what i need to put in place of these bolded lines

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to set a session key all you have to do is
    Code:
    $_SESSION['uid'] = $person['id']
    but this is no more secure than the above. Session data can be read and high-jacked just as easily with a url. You do how ever have a few options such as hashing, encryption or just use a hidden form field and POST with proper validation.
    coming soon sitejuju.com my new development portfolio

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But sir in previous case, individual <a href> were able to identify unique users. How can i do so with session variable ?

    In my above code, user jeff will have its own modify/delete link, user bob will have its own. How can i achieve the same using session ? please a piece of code will be really helpful here

    Thanks alot sir

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,049
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You cannot do this with sessions.

    By clicking a link to modify a user, you utilise GET to tell your code which one was clicked. You'll notice that, when you look at the HTML output, it's written on each individual link.

    When you set a session value, it's all done on the server. Which button is clicked has nothing to do with it. It is not $_SESSION's job to interact with the page, it's just not the right tool for the job.

    What are you trying to achieve? Using $_SESSION for something like this would surely be a step towards solving a problem, but I can assure you it's the wrong step. What is the problem you are trying to solve?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jake's answer is correct sessions is not your answer to your problem. Your options really entail using a click event and ajax in Javascript/Jquery etc... and post the id and validate.. or just using a basic form submit and pass the id with a hidden form field and the ID as the default value and then validate. Your really only have 2 options when passing the data from client side to server side. GET and POST.
    coming soon sitejuju.com my new development portfolio

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    15,819
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Sounds like what you are actually after is HTTPS which will encrypt all the data when it is sent between the browser and the server.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot all for solving my confusion. Basically what i wanted to do was, to display all the users in my database and providing the links to modify and delete them individually. For this reason i asked this.

    Thanks alot again

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •