4

I need to write the contents of an array to a file, each time the page is loaded... I've created the array in index.php and pushing contents to the array in another ajax page.. But i couldn't access the array globally.. its showing an error as 'undefined variable $arr'..

Here's my code..

Index.php page...

<?php
    $arr = array();

    $ourFileName = "saved_data.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    fwrite($ourFileHandle, "");

?>

Ajax page.....

<?php
    $name_full = $_GET['name_full'];
    $arr = $_GET['$arr'];

    array_push($arr,$name_full);
    /*------------To create a file-----------------*/
    $ourFileName = "saved_data.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    /*---------To save data in the file------------*/
    foreach($arr as $key => $value)
    {
        fwrite($ourFileHandle, $value);
    }
    fwrite($ourFileHandle, ',');
    fclose($ourFileHandle);
    echo $name_full;
?>

what else should i do to make this array global...

Deepzz
  • 4,573
  • 1
  • 28
  • 52
  • really looks like you should use a db not a file –  Aug 10 '12 at 04:38
  • Will you explain your need more? By seeing code what you're doing seems correct, why you need to declare an array as global? – WatsMyName Aug 10 '12 at 04:38
  • @Sabin : becoz if i dont do that, i should declare it in ajax page and each time when the page is loaded, it will create a new array.. then how could i add content to it?? – Deepzz Aug 10 '12 at 04:41

3 Answers3

2

in ajax page declare $arr as, global $arr; and see if this works, but i doubt this don't work because each time the page is loaded, the array gets reset, Why dont you use session for this?

WatsMyName
  • 4,240
  • 5
  • 42
  • 73
  • @Deepthi, yes i doubt that. Instead of storing it in array, store it in session. Read session in ajax page and write it to the text file. This is the feasible solution for you. – WatsMyName Aug 10 '12 at 04:49
  • @Deepthi, you're most welcome, its good to help and get helped over here. If my answer helped you, mark this answer as accepted. :) – WatsMyName Aug 10 '12 at 04:59
2

Yeah, variables "expire" after each page is loaded. If you need some data to persist between requests, you have a few options:

  • pass the data to the client (perhaps in a hidden form field), and the have them re-submit it (accessible via GET/POST). This is bad because it's easy for the user to manipulate this data client-side
  • store the variables in $_SESSION which will persist for the user. This is bad because if you have more than one server, data won't be accessible on the other servers (unless you do some fancy load-balancing to ensure clients hit the same server each time)
  • use a 'temporary' store (memcache, redis) which is available to all servers
  • user s 'persistant' store (mySQL, mongo) which is available to all servers
Evan
  • 3,191
  • 4
  • 29
  • 25
1

have you included the index.php in ajax.php?if you have include index.php,then do as "Sabin" says. i don't understand what you want to do.remember that every time you call a php file,it has no matter with previous php file.if you want a global variety in your site,use db will be better

Wenhuang Lin
  • 214
  • 2
  • 8