Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 34
  1. #16
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Quote Originally Posted by Fou-Lu View Post
    Sessions only last until the browser is closed. If you need it to persist, you'll need to implement a datastorage through files or a db in order to block.
    I think the OP means to start again with the current open browser.

    PHP Code:
    session_unset() 
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  2. #17
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    I think the OP means to start again with the current open browser.

    PHP Code:
    session_unset() 
    Should I add it somewhere in the existing code? Or replace something?

  3. #18
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    You could maybe add a submit button?
    PHP Code:
    if(isset($_POST['restart'])) {
        
    session_unset();

    And:

    Code:
    <input type="submit" value="Restart" name="restart" />
    Edit: Changed type="button" to type="submit"
    Last edited by dan-dan; 07-02-2012 at 11:05 PM.
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  4. #19
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    You won't be able to restart from outside your domain unless you open the location in an <object> or <iframe> rather than using a direct redirect.
    Last edited by dan-dan; 07-02-2012 at 11:14 PM.
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  5. #20
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    You could maybe add a submit button?
    PHP Code:
    if(isset($_POST['restart'])) {
        
    session_unset();

    And:

    Code:
    <input type="submit" value="Restart" name="restart" />
    Edit: Changed type="button" to type="submit"
    Hmm I'm not quite sure if I go it. Is it possible to achieve same result without a restart button?

    I can briefly explain my concept:
    I have a main page where I click on a link and random questions start to pop up (questions are on sepperate .html pages with submit buttons to go to the next question). I made a .php file and added this code so I could randomize certain amount of questions and then redirect to main page again. Now everything works fine and does what I need, thanks to all the help

    I was just conserned that If I close the questions' page, and then click on the link again then it does't open me questions' page anymore, but openes me the page where it is already redirected. It might not be a problem. But I was just thinking if someone wants to answer them again, they wouldn't probably think of deleting the cookies or closing the browser.

  6. #21
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Well you'd need some kind of user input to give the user the choice to start over. A button seemed most appropriate.

    Providing each of your question pages used the session, then you could place that restart submit button on the same page with the next button. All this would do is unset the session variable and thus restart the questions.

    You could even have a 'finish page' with that submit button saying something like "You have completed all the questions, click here to start again".
    Last edited by dan-dan; 07-02-2012 at 11:38 PM.
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  7. #22
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Sorry, I don't think I was completely following. You could simply unset the session when all the questions have been visited. Adding to Fou-Lu's code:

    PHP Code:
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
            
    session_unset(); 
            
    header('Location: ' $sRedirectTo);
            exit();
        } 
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  8. #23
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    Sorry, I don't think I was completely following. You could simply unset the session when all the questions have been visited. Adding to Fou-Lu's code:

    PHP Code:
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
            
    session_unset(); 
            
    header('Location: ' $sRedirectTo);
            exit();
        } 
    YES! Exactly what I needed! Huh, thank you a lot!

    I would have one last and tiny question: now the code is written to take all the questions from the folder and randomly output them.
    In what place and what would be the right code snipet to add to take, for example, only 10 questions out of 20?

  9. #24
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    I added 'page' to the session. Think this should work:

    PHP Code:
    <?php
    session_start
    ();
    error_reporting (E_ALL E_STRICT);
    $files = array();
    $path "./randompages/"
    $sRedirectTo 'http://yoursite.com/index.php';
    $_SESSION['page'] = 0;

    if (!isset(
    $_SESSION['visited']))
    {
        
    $_SESSION['visited'] = array();
    }

    if (
    $dh = @opendir($path))
    {
        while (
    false !== ($file readdir($dh)))
        {
            if (
    $file != "." && $file != ".." && is_file($path $file))
            {
                
    $files[] = $path $file;
            }
        }
        
    closedir($dh);
        
    // mkay, here remove what's already been visited
        
    $aUnvisited array_diff($files$_SESSION['visited']);
        
    print_r($aUnvisited);
        if (
    count($aUnvisited) > 0)
        {
            
    // Still have places to go
            
    shuffle($aUnvisited);
            
    $sToInclude array_shift($aUnvisited);
            
    $_SESSION['visited'][] = $sToInclude;
            include(
    $sToInclude);
            echo 
    "You are on question " $_SESSION['page'] += " of " count($files) ;
        }
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
            
    session_unset(); 
            
    header('Location: ' $sRedirectTo);
            exit();
        }
    }
    else
    {
        
    printf("Could not open %s for reading" PHP_EOL$path);

    ?>
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  10. #25
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    I added 'page' to the session. Think this should work:

    PHP Code:
    <?php
    session_start
    ();
    error_reporting (E_ALL E_STRICT);
    $files = array();
    $path "./randompages/"
    $sRedirectTo 'http://yoursite.com/index.php';
    $_SESSION['page'] = 0;

    if (!isset(
    $_SESSION['visited']))
    {
        
    $_SESSION['visited'] = array();
    }

    if (
    $dh = @opendir($path))
    {
        while (
    false !== ($file readdir($dh)))
        {
            if (
    $file != "." && $file != ".." && is_file($path $file))
            {
                
    $files[] = $path $file;
            }
        }
        
    closedir($dh);
        
    // mkay, here remove what's already been visited
        
    $aUnvisited array_diff($files$_SESSION['visited']);
        
    print_r($aUnvisited);
        if (
    count($aUnvisited) > 0)
        {
            
    // Still have places to go
            
    shuffle($aUnvisited);
            
    $sToInclude array_shift($aUnvisited);
            
    $_SESSION['visited'][] = $sToInclude;
            include(
    $sToInclude);
            echo 
    "You are on question " $_SESSION['page'] += " of " count($files) ;
        }
        else
        {
            
    // Everything has been visited.  Put your end case scenario here.
            
    session_unset(); 
            
    header('Location: ' $sRedirectTo);
            exit();
        }
    }
    else
    {
        
    printf("Could not open %s for reading" PHP_EOL$path);

    ?>
    Actually it only made the code visible on the background of the questions (showing on what question I am and how many left etc). It didn't give me an opportunity to have only few questions out of 20, even if I changed that 0 in the end of that 'page' session o 10. But I probably did it wrong?

  11. #26
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    Sorry my bad, again I misread. So you want to allow the user to choose a specific amount of questions to answer? How would you want this presented? Drop down, text input etc?
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  12. #27
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    Sorry my bad, again I misread. So you want to allow the user to choose a specific amount of questions to answer? How would you want this presented? Drop down, text input etc?
    No problem. I apologize if I can't explain myself very well.

    That is also not exactly what I want.

    Let's say I have for example 40 pages in the folder, and now what my php code does, is shows all of the pages randomly, without repeating and when all 40 are shown, it redirects to randompage.com.

    What I would like, if it is possible, that the code would automatically choose only random 20 of those 40 pages to show randomly (so there is more chance each time when someone answers, that different questions pop up) and then redirect to randompage.com

    Does it sound more clear? English is not my native language, it might sound clear to me, but weird to someone else

  13. #28
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    But whether you choose 20 or 40 questions they're all still random, and they're all still only going to occur once per session. Unless I'm missing something here (which is very likely ) it seems like a pointless task.
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein

  14. #29
    New Coder
    Join Date
    May 2012
    Posts
    57
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    But whether you choose 20 or 40 questions they're all still random, and they're all still only going to occur once per session. Unless I'm missing something here (which is very likely ) it seems like a pointless task.
    Yap I want them to be random.

    For example when I have 5 pages and only 2 are pulled
    and one person answers the for the first time and gets pages:
    1,3
    if the same person for some reson wants to answer again, he or she might get pages:
    5,2
    and so on.

    Maybe it is pointless though, im not sure yet But that would increase the chance that the person will get a new question not all the same as before
    Is that possible? :P

  15. #30
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    OK, I get you. I was under the impression you were going through the entire questions folder for every session.
    I'll have a look.
    Logic will get you from A to B. Imagination will take you everywhere.
    Albert Einstein


 
Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •