Passing data from one php program to another

by 8 replies
10
I am trying to pass data from one php file to another. I have tried sessions and cookies and neither works.

Here is what I want to do:

test1.php
<?php session_start(); ?>
<html>
<body>
<?php

$_SESSION['testparm']='Hello';

?>
<a href="http://www.mydomain.com/test2.php">click here</a>;
</body>

-------
test2.php
<body>
<?php echo $_SESSION[['testparm']; ?>
</body>

I show the example with SESSION but Cookies are ok too, if they work. Right now I can't get either to work.

I'm unclear as to whether I should call session_start() in test2.php, but I have tried both ways.

Help please?

thanks,
Roger.
#programming #data #passing #php #program
  • Yes, you need to call session_start(). What browser are you using?

    Also, hae you tried moving the session_start() right above the...

    $_SESSION['testparm']='Hello';

    ... line?
  • I'm using Firefox (3.5.5).

    I put session_start() as the top line, otherwise it seems to puke and tell me the headers have already been modified...

    I tried this:

    ---- top of program ---
    <?PHP session_start(); ?>
    <?php setcookie('test', 'hello', time()+3600) ?>
    <?PHP $_SESSION['test2']='hello2'; ?>
    <html>
    link to page2



    --- page2.php
    <?PHP
    session_start();
    $test = '';
    $test = $_SESSION['test2'];
    echo $test;
  • Right, you don't need to keep using the <php ?> tags as you have shown above for page1.php Just one lot to surround your tags. You shouldn't get any header problems.

    Also, you do not have to declare the $test variable first. You are doing that when you store the session variable to it.

    Do you need to use sessions or cookies for this? I would certainly not use a cookie for this. Have you thought about using $_GET instead and passing the content through the URL?
    • [1] reply
    • thanks, I do know that, but I was copying & pasting around to try things and wanted everything contained on a line...

      I am trying to avoid the URL method. Eventually I want to be able to do this in WordPress and not have to mess with Exec-PHP.

      I don't care if sessions or cookies; just figured there had to be a way to make it work.

      Would a form work or would everything have to be visible and look like a form?

      thanks
  • With a form you could pass the data via a 'hidden' field but you'd still need to send the form data by clicking a button or performing an action.

    This should just work.....

    <?php
    session_start();
    $_SESSION['test'] = "Test";
    ?>
    // Rest of html etc here

    Then page2.php

    <?php
    session_start();
    $test = $_SESSION['test'];
    ?>
    // Rest of html etc here

    Without using a form or passing data through the URL you are best using sessions. Have you tried using a different browser? Are you using Chrome?
  • Thanks for the help, I'll give it another try.

    I actually need something that 99% of browsers will do successfully, but so far I have only tried Firefox.

    thanks,
    Roger.
  • Works in IE7

    Does NOT work in Firefox

    Any idea why or how to fix?

    thanks.
  • Hi,

    Perhaps the simplest thing to do would be to add a parameter to the end of your URL in you <a>. So, you have:

    <a href="http://www.mydomain.com/test2.php?param=hello">click here</a>;

    Then in the php file test2.php to access the param simply use:

    $myParam = $_GET["param"];

    That's it. You could probably use isset to make sure "param" is set before trying to get a value from it.

    Cheers, Lee.

Next Topics on Trending Feed