Passing a Variable to a new page

Discussion in 'PHP' started by swapshop, Jun 4, 2008.

  1. #1
    Need some advice on how to pass a variable to a new page

    <u><a href='user-feedback.php?user_id={$user_id}")'><u>Check Users Feedback</a></u></small>

    the variable here that I need is the on the advert details page called $user_name

    The link above when clicks goes to a new page to allow the person to place feedback on the seller.

    When on the details page the $user_name variable is there say sarah

    I want to use the $user_name to pre fill the form name on the feedback page

    How do I page the variable to a new page?

    Ie is there like a global variable?

    Issue is due to too totally separate scripts being used. But all I need is to have the variable from page on passed to page two
     
    swapshop, Jun 4, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are three arrays - $_POST, $_GET, $_REQUEST. Those are filled with the variables that the web page submitted during the last request. Google them a little bit and you'll find it really easy
     
    xlcho, Jun 4, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You can also register it as a session variable. This way the user can't see the variable but you can still access it through the $_SESSION superglobal.

    On the page that you set / access user_id:
    
    session_start();
    
    $_SESSION['user_name'] = 'Sarah';
    
    
    PHP:
    On any other page after the session variable has been set:

    
    session_start();
    
    echo $_SESSION['user_name'];
    
    
    PHP:
    Make sure session_start() is the first thing called on the page.
     
    jestep, Jun 4, 2008 IP
  4. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #4
    jestep thanks can you pass more than one variable in the session I actually need three by the looks of it?


    xlcho thanks
     
    swapshop, Jun 4, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    You can pass as many as you need. You can also pass an array if that is more appropriate for your situation.

    SOmething like:
    
    
    $_SESSION['user'] = array('name'=>'John', 'Age'=>54, 'Website'=>'mysqite.com');
    
    
    //access these like...
    echo $_SESSION['user']['name'];
    
    PHP:
     
    jestep, Jun 4, 2008 IP
  6. Tom__

    Tom__ Peon

    Messages:
    148
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Mmmm Thanks, didn't knew that. :)
     
    Tom__, Jun 4, 2008 IP
  7. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok how do I take smarty templates into account?

    the page that holds the variables I need is

    details.php calls header_details.php which has this session info

    if (isset($_SESSION["valid_user"]))
    {
    $tpl->assign("member",$_SESSION["valid_user"]);
    $tpl->assign("member_name",$_SESSION["user_name"]);
    }
    $tpl->display("header_detail.tpl");

    this then generates details.tpl

    If I add {debug} I can see all the page variables that some of them I need

    so on the details.tpl (generated from details.php) I have a link

    <a href="javascript:eek:penWin2('sms/index.php?user_id={$user_id}&amp;sms=1',500,300)" id="addfav"> SMS</a>

    this opens a page with the form and I need to popuplate some of the form from the details.tpl variables like user name and cell number?

    Any more ideas?

    I understand about the session but I need to know how to deal with smarty as well although the popup sms page is just php and not a tpl (smarty)?
     
    swapshop, Jun 6, 2008 IP
  8. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Can you tell me where I put the session in here?


    <?php

    /**
    * GentleSource Comment Script - session.class.inc.php
    *
    * @copyright (C) Ralf Stadtaus , {@link http://www.gentlesource.com/}
    *
    */




    /**
    * Session handler
    */
    class c5t_session
    {




    var $session_vars;
    var $session_vars_name; // Name of the session array that contains all stored data

    // -----------------------------------------------------------------------------




    /**
    * Constructor
    *
    * @access private
    */
    function c5t_session()
    {
    $this->session_vars_name = 'MY_SESS';
    }

    // -----------------------------------------------------------------------------




    /**
    * Set session vars name
    *
    * @access private
    */
    function sess_set_vars_name($name)
    {
    $this->session_vars_name = $name;
    }

    // -----------------------------------------------------------------------------




    /**
    * Start session
    *
    * @access private
    */
    function sess_start($sess_id = 0, $sess_name = '')
    {
    if (session_id() == '') {
    session_set_cookie_params(false, '/');
    session_start();
    }

    if (!isset($_SESSION[$this->session_vars_name])) {
    $_SESSION[$this->session_vars_name] = array();
    }

    $this->session_vars = $_SESSION[$this->session_vars_name];
    }

    // -----------------------------------------------------------------------------




    /**
    * Register vars
    *
    * @access private
    */
    function sess_register($data = array())
    {
    if (sizeof($data) <= 0) {
    return false;
    }

    while (list($key, $val) = each($data))
    {
    $_SESSION[$this->session_vars_name][$key] = $val;
    $this->session_vars[$key] = $val;
    }
    return $this->session_vars;
    }

    // -----------------------------------------------------------------------------




    /**
    * Stop session
    *
    * @access private
    */
    function sess_stop($sess_id = 0, $sess_name = '')
    {
    unset($this->session_vars);
    unset($_SESSION[$this->session_vars_name]);
    if (isset($GLOBALS[$this->session_vars_name])) {
    unset($GLOBALS[$this->session_vars_name]);
    }
    }

    // -----------------------------------------------------------------------------




    /**
    * Start session
    *
    * @access private
    */
    function start()
    {
    global $c5t;
    if (!isset($GLOBALS['session_object'])) {
    $sess = new c5t_session();
    $sess->sess_set_vars_name($c5t['session_vars_name']);
    $sess->sess_start();

    $GLOBALS['session_object'] = $sess;
    }
    if (isset($GLOBALS['session_object'])) {
    return $GLOBALS['session_object'];
    }
    }

    // -----------------------------------------------------------------------------




    /**
    * Add values to session
    *
    * @access public
    * @param array $data Array of data to be
    */
    function add($data)
    {
    $sess = c5t_session::start();
    $sess->sess_register($data);
    $GLOBALS['session_object'] = $sess;
    }

    //------------------------------------------------------------------------------




    /**
    * Get session data
    *
    * @access public
    * @return bool|array Returns session data if session exists or false
    */
    function get($value = null)
    {
    $sess = c5t_session::start();

    if ($value == null) {
    return $sess->session_vars;
    }
    if (isset($sess->session_vars[$value])) {
    return $sess->session_vars[$value];
    }
    }

    //------------------------------------------------------------------------------




    /**
    * Destroy session
    *
    * @access public
    */
    function destroy()
    {
    $sess = c5t_session::start();
    $sess->sess_stop();
    }

    //------------------------------------------------------------------------------









    }






    ?>
     
    swapshop, Jun 21, 2008 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    It would be something like this:

    
    //at the top of the document
    $session = new c5t_session;
    
    //to set a variable
    $session->add(array('key','value')); 
    
    //to get a session variable
    $session_variable =  $session->get('key');
    
    
    PHP:
     
    jestep, Jun 21, 2008 IP
  10. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #10
    jestep thanks for your suggestion to get me started again

    I tried your original way but of course the comment script has registered sessions running.

    The comment script is actually written in smarty templates as well but i just need to be able to pass the user name from the classifieds script to the comments script so I can pre populate a form with the users name.

    Will post back how I get on
     
    swapshop, Jun 21, 2008 IP