Passing data from one php program to another

8 replies
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.
#data #passing #php #program
  • Profile picture of the author AndyBlackSEO
    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?
    Signature
    [FREE SEO TOOL] Build 29 Effective, High Authority Backlinks that Will Increase Your Google Rankings in 2020... CLICK HERE ...
    ... Instant backlinks that can get you results within 24-72hrs.
    {{ DiscussionBoard.errors[1816262].message }}
  • Profile picture of the author PCRoger
    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;
    Signature
    Track your affiliate sales back to the ARTICLE or WEBSITE that generated the sale. CBSaleTracker

    I was making money in days with the 4 Day Money Making Blueprint

    {{ DiscussionBoard.errors[1816286].message }}
  • Profile picture of the author AndyBlackSEO
    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?
    Signature
    [FREE SEO TOOL] Build 29 Effective, High Authority Backlinks that Will Increase Your Google Rankings in 2020... CLICK HERE ...
    ... Instant backlinks that can get you results within 24-72hrs.
    {{ DiscussionBoard.errors[1816305].message }}
    • Profile picture of the author PCRoger
      Originally Posted by AndyBlackSEO View Post

      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.
      thanks, I do know that, but I was copying & pasting around to try things and wanted everything contained on a line...

      Originally Posted by AndyBlackSEO View Post

      Do you need to use sessions or cookies for this? I would certainly not use a cookie for this. Have you thought about using Array instead and passing the content through the URL?
      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
      Signature
      Track your affiliate sales back to the ARTICLE or WEBSITE that generated the sale. CBSaleTracker

      I was making money in days with the 4 Day Money Making Blueprint

      {{ DiscussionBoard.errors[1816331].message }}
  • Profile picture of the author AndyBlackSEO
    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?
    Signature
    [FREE SEO TOOL] Build 29 Effective, High Authority Backlinks that Will Increase Your Google Rankings in 2020... CLICK HERE ...
    ... Instant backlinks that can get you results within 24-72hrs.
    {{ DiscussionBoard.errors[1816354].message }}
  • Profile picture of the author PCRoger
    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.
    Signature
    Track your affiliate sales back to the ARTICLE or WEBSITE that generated the sale. CBSaleTracker

    I was making money in days with the 4 Day Money Making Blueprint

    {{ DiscussionBoard.errors[1816625].message }}
  • Profile picture of the author PCRoger
    Works in IE7

    Does NOT work in Firefox

    Any idea why or how to fix?

    thanks.
    Signature
    Track your affiliate sales back to the ARTICLE or WEBSITE that generated the sale. CBSaleTracker

    I was making money in days with the 4 Day Money Making Blueprint

    {{ DiscussionBoard.errors[1816912].message }}
  • Profile picture of the author lharding
    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.
    Signature
    Lee Harding
    The Architect
    {{ DiscussionBoard.errors[1817033].message }}

Trending Topics