Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-02-2012, 09:36 PM   PM User | #16
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Old 07-02-2012, 09:41 PM   PM User | #17
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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?
krispol is offline   Reply With Quote
Old 07-02-2012, 09:56 PM   PM User | #18
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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"
__________________
Logic will get you from A to B. Imagination will take you everywhere.
Albert Einstein

Last edited by dan-dan; 07-02-2012 at 10:05 PM..
dan-dan is offline   Reply With Quote
Old 07-02-2012, 10:01 PM   PM User | #19
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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.
__________________
Logic will get you from A to B. Imagination will take you everywhere.
Albert Einstein

Last edited by dan-dan; 07-02-2012 at 10:14 PM..
dan-dan is offline   Reply With Quote
Old 07-02-2012, 10:21 PM   PM User | #20
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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.
krispol is offline   Reply With Quote
Old 07-02-2012, 10:35 PM   PM User | #21
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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".
__________________
Logic will get you from A to B. Imagination will take you everywhere.
Albert Einstein

Last edited by dan-dan; 07-02-2012 at 10:38 PM..
dan-dan is offline   Reply With Quote
Old 07-02-2012, 10:46 PM   PM User | #22
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Old 07-02-2012, 10:53 PM   PM User | #23
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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?
krispol is offline   Reply With Quote
Old 07-03-2012, 06:28 PM   PM User | #24
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Old 07-03-2012, 07:10 PM   PM User | #25
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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?
krispol is offline   Reply With Quote
Old 07-03-2012, 07:29 PM   PM User | #26
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Old 07-03-2012, 07:36 PM   PM User | #27
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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
krispol is offline   Reply With Quote
Old 07-03-2012, 07:50 PM   PM User | #28
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Old 07-03-2012, 07:56 PM   PM User | #29
krispol
New Coder

 
Join Date: May 2012
Posts: 56
Thanks: 12
Thanked 0 Times in 0 Posts
krispol is an unknown quantity at this point
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
krispol is offline   Reply With Quote
Old 07-03-2012, 08:17 PM   PM User | #30
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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
dan-dan is offline   Reply With Quote
Reply

Bookmarks

Tags
array, php, random, randomize

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:36 AM.


Advertisement
Log in to turn off these ads.