How to pass variables from one php file to another without including it?

Discussion in 'PHP' started by Make a perfect site, Jul 19, 2011.

  1. #1
    Hi all,

    I'm working on a new project but I'm stuck.
    I'm stuck with passing variables.

    Example:

    file1.php :
    
    <?php 
    $id ="abc123";
    ?>
    
    PHP:
    file2.php :
    
    <?php
    echo $id;
    ?>
    
    PHP:
    I want to make this happen :). What is the right piece of code for it? (I don't want to 'include' the whole file)

    Thanks,

    Akos
     
    Make a perfect site, Jul 19, 2011 IP
  2. clarkmilark

    clarkmilark Member

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    I have this same issue! Can someone post a solution?
     
    clarkmilark, Jul 19, 2011 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    file1.php sends the id number like this file2.php?id=$id

    file2.php
    
    <?php 
    $id = $_GET['id'];
    
    echo $id;
    
    ?>
    
    Code (markup):
     
    MyVodaFone, Jul 19, 2011 IP
  4. Make a perfect site

    Make a perfect site Well-Known Member

    Messages:
    376
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    155
    #4
    Thanks so much! I've got it working. :)
     
    Make a perfect site, Jul 19, 2011 IP
  5. CrostE

    CrostE Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Remember to make the script check if the ID exists.
     
    CrostE, Jul 19, 2011 IP
  6. suryawl

    suryawl Peon

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #6
    you can redirect from file1.php to file2.php with this : header("Location: file2.php?id=$id");
     
    suryawl, Jul 22, 2011 IP
  7. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Please take a look at the php session information. It is meant to pass the vars to different php files.

    http://www.php.net/manual/en/function.session-start.php


    
    <?php
    // page1.php
    
    session_start();
    
    echo 'Welcome to page #1';
    
    $_SESSION['favcolor'] = 'green';
    $_SESSION['animal']   = 'cat';
    $_SESSION['time']     = time();
    
    // Works if session cookie was accepted
    echo '<br /><a href="page2.php">page 2</a>';
    
    // Or maybe pass along the session id, if needed
    echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
    ?>
    
    PHP:
    
    <?php
    // page2.php
    
    session_start();
    
    echo 'Welcome to page #2<br />';
    
    echo $_SESSION['favcolor']; // green
    echo $_SESSION['animal'];   // cat
    echo date('Y m d H:i:s', $_SESSION['time']);
    
    // You may want to use SID here, like we did in page1.php
    echo '<br /><a href="page1.php">page 1</a>';
    ?>
    
    PHP:
    If you don't want to have to session_start each time and this is only for your personal server then you could use the php.ini to auto start the session for each user connection.

    See below for the var needed to be set to true, 1 in the php.ini file.
    session.auto_start boolean - session.auto_start specifies whether the session module starts a session automatically on request startup. Defaults to 0 (disabled).
     
    exodus, Jul 22, 2011 IP