Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:22:34+00:00 2026-06-06T16:22:34+00:00

I hope I worded the title accurately enough but I typically use Java and

  • 0

I hope I worded the title accurately enough but I typically use Java and don’t have much experience in Web Development/PHP/CodeIgniter. I have a difficult time understanding the life cycle of a script as I found out trying to implement a certain feature to a website I am developing (as a means of learning how to). I’ll first describe the feature I tried implementing and then the problem I ran into that made me question my fundamental understanding of how scripts work since I’m used to typical OOP.

Ok so here goes…

I have a webpage that has 2 basic tasks a user can do, create and delete an entry. What I attempted to implement was a way to time a user how long it takes them to complete a certain task. The way I did this was have a homepage where there would be a list of tasks a user to choose from (in this case 2, create and delete). A user would click a task which would link to the ‘true’ homepage where the user then would be expected to complete the task. My script looks like this:

<?php

class Site extends CI_Controller {

var $task1;
var $tasks = array(
                        "task1" => NULL,
                        "date1" => 0,
                        "date2" => 0,
                        "diff" => 0);

function __construct()
{
    parent::__construct();
    include 'timetask.php';
    $this->task1 = new TimeTask("create");      
}   

function index() 
{   
    $this->tasks['task1'] = $this->task1->getTask();
    $this->tasks['diff'] = $this->task1->getTimeDiff();

    if($this->tasks['diff'] == NULL)
    {
            $this->tasks['diff'] = 0;
    }   

    $this->load->view('usability_test', $this->tasks);
}

function origIndex() 
{   
    $this->task1->setDate1(new DateTime());
    $this->tasks['date1'] = $this->task1->getDate1()->getTimestamp();
    $data = array();

    if($q = $this->site_model->get_records())
    {
        $data['records'] = $q;
    }

    $this->load->view('options_view', $data);
}

function create()
{
    $this->task1->setDate2(new DateTime());
    $this->tasks['date2'] = $this->task1->getDate2()->getTimestamp();

    $data = array(
            'author' => $this->input->post('author'),
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents')
    );  

    $this->site_model->add_record($data);
    $this->index();

}

I only included create to keep it short. Then I also have the TimeTask class, that actually another StackOverflow so kindly helped me with:

<?php

class TimeTask
{
private $task;

/**
 * @var DateTime
 */
private $date1, $date2;

function __construct($currTask) 
{
    $this->task = $currTask;
}

public function getTimeDiff() 
{
    $hasDiff = $this->date1 && $this->date2;
    if ($hasDiff) {
        return $this->date2->getTimestamp() - $this->date1->getTimestamp();
    } else {
        return NULL;
    }
}

public function __toString() 
{
    return (string) $this->getTimeDiff();
}

/**
 * @return \DateTime
 */
public function getDate1()
{
    return $this->date1;
}

/**
 * @param \DateTime $date1
 */
public function setDate1(DateTime $date1)
{
    $this->date1 = $date1;
}

/**
 * @return \DateTime
 */
public function getDate2()
{
    return $this->date2;
}

/**
 * @param \DateTime $date2
 */
public function setDate2(DateTime $date2)
{
    $this->date2 = $date2;
}

/**
 * @return get current task
 */
 public function getTask()
 {
        return $this->task;
 }

}

?>

I don’t think posting the views is necessary for the question but here is atleast how the links are made.

<?php echo form_open('site/create');?>
...and...
<?php echo anchor("site/delete/$row->id", $row->title); ?>

Now there’s no error in the code but it doesn’t do what I expect of it and the reason I assume why is because that each time a function of the script is called via a new page it is NOT the same instance of the script called previously so any previously created objects are no longer there. This confuses me and leaves me quite unsure of how to implement this gracefully. Some ways I would guess of how to do this is by passing the necessary data through the URL or have data saved in a database and retrieve it later to compare the times. What would be a recommended way to do, not just this, but anything that needs previously created data? Also, am I correct to think that a script is only ‘alive’ for one webpage at a time?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:22:35+00:00Added an answer on June 6, 2026 at 4:22 pm

    Web development is a bit different to “standard” development – principally because of the nature of HTTP. Each request to the web application has to travel across the network using HTTP, which, as all web developers know, is stateless. What this means is that web servers do not have to remember anything about previous HTTP requests. Usually, webdevs get round this using cookies in one way or another – where a cookie is some bit of data, coded as a text string, which is sent back to the browser so that it can resend it to the application on the next request. Like that, a cookie is a kind of transferable memory.

    So, each time you make a request, unless you transfer some data using a cookie (either an HTTP cookie, or what is sometimes called a URL cookie – state data coded in the URL), it looks to the web application like a brand new request, unrelated to any past request. So, for your application to work, you need to use a cookie in some way to remember or recover the start time when you detect that the user has finished a task. You can either (i) use CI’s built-in facilities for remembering data (flashdata, as mentioned above, or userdata from the CI Session class – see http://codeigniter.com/user_guide/libraries/sessions.html), which are built on top of CI cookies), (ii) do this using your own cookie data (not recommended – why use the framework in that case?), or (iii) use hidden form fields – an oldie but sometimes goldie technique that requires the PHP script generating a view to write hidden form fields whose values are the data you want to remember and have sent back to you on the next request.

    This kind of problem is something you’ll come across again and again in web development – so get to know the problem and its solutions well!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope I worded the title of my question appropriately. In c# I can
I hope this is the appropriate title for this question. Right now I have
Hope you guys don't mind me asking this question, but I find myself at
I hope I have worded the question ok. I know that you can pass
Hope that someone can share their experience with some of the latest emerging backbone.js
Hope this question is not stupid since I am an amateur web designer. I
Hope the AWK gurus can provide a solution to my problem . I have
hope all is swell! I was hoping I had cracked this problem but it
Hope someone could help me with this: I would like to have a column
Hope someone will give me a hand with this problem I have. So here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.