2

I understand that array_push is not adding a 2nd item because when the page refreshes after adding another item the original one goes away and get's replaced by the most recent entry from the text box.

I am trying to achieve this tactic of trying to either...

a. Have a session remember the next item being added EVEN through a page refresh.

or

b. Just not refresh the page at all.

See demo here: http://query.notesquare.me/home.html/

CODE

<form method="post">
    <input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
    <input type="submit" id="button-create-playlist" value="Create Playlist" />
</form>

<?php
    session_start();

    $create_playlist = $_POST['create_playlist'];

    $playlists = array("One", "Two", "Three");

    $_SESSION['main'] = $playlists;

    array_push($playlists, $create_playlist);

    for ($i = 0; $i < count($playlists); $i++) {
        echo $playlists[$i] . "<br />";
    }
?>
mistkaes
  • 399
  • 1
  • 2
  • 21
  • You need to move up the array_push before setting the $_SESSION variable. And if you want to store more than the last playlist you need to read from the $_SESSION variable, add the new playlist, save to the $_SESSION variable. – mindore Apr 12 '15 at 08:45
  • Or just save directly to the session $_SESSION['main'][] = $create_playlist; And use the $_SESSION variable in the for loop. – mindore Apr 12 '15 at 08:52

1 Answers1

2

Try

<form method="post">
    <input type="text" id="input-create-playlist" placeholder="Playlist Name" name="create_playlist" />
    <input type="submit" id="button-create-playlist" value="Create Playlist" />
</form>

<?php
    session_start();

    $create_playlist = $_POST['create_playlist'];

    $_SESSION['user_playlists'][] = $create_playlist;

    $playlists = array("One", "Two", "Three");

    for ($i = 0; $i < count($_SESSION['user_playlists']); $i++) {
        array_push($playlists, $_SESSION['user_playlists'][$i]);
    }

    $_SESSION['main'] = $playlists;      

    for ($i = 0; $i < count($playlists); $i++) {
        echo $playlists[$i] . "<br />";
    }
?>

Your approach was setting the $_SESSION['main'] = $_POST['create_playlist'] before the desired effects of array_push.

sitilge
  • 3,687
  • 4
  • 30
  • 56
  • Ok, it seems that there are problems with the php settings. Could you please some of the session related variables. You could phpinfo() but take this in account: http://stackoverflow.com/questions/3196011/what-security-problems-could-come-from-exposing-phpinfo-to-end-users – sitilge Apr 12 '15 at 18:58
  • I printed the settings of my PHP here: http://query.notesquare.me/, Do you think you can help me target the problem? Maybe something is disabled under Sessions or Cookies? – mistkaes Apr 12 '15 at 19:27
  • The problem could be /tmp directory: there might not be writing permissions for the user apache runs as. Do `sudo chmod 775 /tmp`. – sitilge Apr 12 '15 at 20:21
  • Also check what user apache is running as `lsof -i | less` – sitilge Apr 12 '15 at 20:27
  • What would I check "sudo chmod 775 /tmp" under, I know there's a TMP file but where would I check for what? – mistkaes Apr 13 '15 at 03:24
  • With `sudo chmod 775 /tmp` you change permission of directory /tmp. Your httpd process user might not have permissions to write to that directory - the user might not be in the group that has write permissions. So with `lsof -i | less` you can check the user and then with `ls -ad /tmp` check the permissions. – sitilge Apr 13 '15 at 07:00
  • Alright, I changed the permissions to 775 and checked, and they are indeed 775. Although the code is still not working – mistkaes Apr 13 '15 at 07:44
  • What is your httpd user and to what group does the user belong? – sitilge Apr 13 '15 at 08:02
  • The code was working ever since the second edit, the problem on your side. – sitilge Apr 13 '15 at 08:03
  • What is the directory for httpd user? and how would I check it? – mistkaes Apr 13 '15 at 20:11
  • httpd is is a process that is owned by some user. By default, if you are using apache, the user would be www-data. And there is no directory (i mean user home directory) for www-data. You have not answered previous comments: check lsof -i | less and ls -ad /tmp ! – sitilge Apr 14 '15 at 06:19
  • Have you any answers yet? – sitilge Apr 14 '15 at 12:06
  • How do I check it, and where can I check this... I'm absolutely 100% confused as to if this is a "console-like" thing that I can just enter and get data from. I'm using GoDaddy cPanel btw, but it should be there... I just don't know how to find it at all! – mistkaes Apr 15 '15 at 18:34
  • Do you have /tmp directory at all? What are /tmp permissions? – sitilge Apr 15 '15 at 20:08
  • Yes, there is a tmp directory. Where should I be looking? – mistkaes Apr 15 '15 at 20:14
  • What are the /tmp permissions? – sitilge Apr 15 '15 at 20:21
  • The permissions for the tmp directory are 775, like you said. I right clicked on it and set the permissions from 755 to 775 – mistkaes Apr 15 '15 at 20:24
  • Ok, try to set to to 777. – sitilge Apr 15 '15 at 20:33
  • Here is my TMP directory and the permissions for the folder, It still doesn't work. http://i.imgur.com/8NehXjn.png – mistkaes Apr 15 '15 at 21:43
  • can you check /var/log/apache2/error.log (the default apahce2 error path for most linux distros) – sitilge Apr 15 '15 at 21:50
  • Where are the logs saved? Check the log file for errors. Print me the output. – sitilge Apr 15 '15 at 21:57
  • 눀걞U흍oۆ毽䎱P㻍M഍Ȱᨁq膈"9Ⓔ�KԾj(별Ꮛl繹ܙٝٝ錬㘦*涍νǯB%OoW钒"/䩴뎅*ğѤُӨ鞏篓ˉ䄈䉣ɹ$꼜ϯ먉嬱n闺ꭚ阞D믅ḋ敟卲.龱⃼.۫/㫥"柶،0⯤鿤... Basically... Giberish... – mistkaes Apr 15 '15 at 22:31
  • Ok, try to make 777 (probably, too much) dir and ini_set(session.save_path, '/path/to/your/dir') – sitilge Apr 15 '15 at 22:47
  • 1. Yes, it can 2. No, make another folder, for example, under the same directory that contains the index.php file – sitilge Apr 15 '15 at 22:58
  • This is the snippet of code I put before session_start()... `ini_set(session.save_path, "/tmp");` – mistkaes Apr 15 '15 at 22:59
  • No, make another dir, for example, under the same dir that contains the index.php file – sitilge Apr 15 '15 at 23:00
  • So you want me to make another directory called "tmp" and then inside that tmp directory, create a file called "index.php" which is where I put `ini_set(session.save_path, "/tmp");`? – mistkaes Apr 15 '15 at 23:02
  • You can name it whatever you want to. Are you kidding me? – sitilge Apr 15 '15 at 23:03
  • Can you just link me a page or stackoverflow to how to do this? – mistkaes Apr 15 '15 at 23:05
  • What is the path to your index.php ? I will write it for you. – sitilge Apr 15 '15 at 23:07
  • Not how to make linux directory commands, how to properly have sessions stored correctly. – mistkaes Apr 15 '15 at 23:07
  • Next time just use google: http://stackoverflow.com/questions/14604589/how-to-use-session-start-properly-on-godaddy-site – sitilge Apr 15 '15 at 23:08
  • There is no index.php – mistkaes Apr 16 '15 at 02:02
  • Since you are using Apache/2.4.12 (Unix), then I am asking you what is the DocumentIndex of http://query.notesquare.me/. In a very, very simple way: what is the file that is opened when you go to http://query.notesquare.me/ ? – sitilge Apr 16 '15 at 07:38
  • home.html is the file that gets opened. – mistkaes Apr 16 '15 at 07:40
  • Go to the directory where home.html is located. Write down the path to this directory, for example, /var/www/notesquare/. Create a new directory 'sessions'. Write this in the file home.html: ini_set("session.save_path", "/var/www/notesquare/home.html"); before session_start(). – sitilge Apr 16 '15 at 07:50
  • Shouldn't I be saving the sessions to the sessions directory, not back to the home.html? Because essentially what that comment is telling me is... to find the directory of my main home.html in query.notesquare.me and then create a new directory called "sessions" and then inside home.html do the ini_set, which essentially leaves the sessions directory empty. – mistkaes Apr 16 '15 at 18:36
  • You're telling me to create a new directory called 'sessions' but not telling me what to do with it or what to put into it, so I'm just leaving it empty which doesn't seem right... – mistkaes Apr 16 '15 at 18:40
  • It is clearly written there: No such file or directory /public_html/query.notesquare.me/home.html//sess_4fd0jbd03kgboaqsjm5ph54nb. So create directory 'whatever' under /home/kucerajacob/public_html/query.notesquare.me/ like /home/kucerajacob/public_html/query.notesquare.me/whatever and set permissions to 777. Later ini_set("session.save_path", " /home/kucerajacob/public_html/query.notesquare.me/whatever"); – sitilge Apr 16 '15 at 21:08
  • Ok, that makes a lot more sense - I will try it when I get home! – mistkaes Apr 16 '15 at 21:15
  • You deserve way more, I can't upvote because I only have 6 reputation – mistkaes Apr 16 '15 at 21:39