1

Forgive me if I am not clear on this but here is what I am trying to accomplish. I have two php pages that each generate a separate json file when each form is submitted. What I want to do is require the main.php to be filled out before the second.php can be filled out. So, if the user goes to the second.php before they fill out the main.php it will redirect them to the main.php. So I need the second.php to "search" for the path to the main.json file and, if empty, redirects back to the main.php. Here is a portion of the code that generates the main.json. The json files are saved on my website.

    require_once $this->site_path . 'inc' . DIRECTORY_SEPARATOR . 'json_file.class.php';
    $jsonFile = new JsonFile();
    $json_result = $jsonFile->writeFile(
            array(
                'file_name'     => $this->site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $file_data['username'] . DIRECTORY_SEPARATOR . 'main.json',//$this->json_path . DIRECTORY_SEPARATOR . $file_data['username']. '_'. 'main.json',
                'data'          => $obj
            )
    );

    $result['json_file'] = $json_result;

    // Save the uploaded file
    if (!empty($file_data['profilebackground']['size'])) {
  $target = $this->site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $file_data['username']. DIRECTORY_SEPARATOR . $file_name;
        if (!move_uploaded_file($file_data['profilebackground']['tmp_name'], $target)) {
            $result['image_file'] = 'There was an error saving the home page image. Please check the image directory permissions and try again.';
            error_log('File ' . $file_data['profilebackground']['name'] . ' not saved: check the image directory permissions.');
        } else {
    include_once("php_img_lib.php");
    img_resize($target, $target, 1000, 1000, $ext);
            $result['image_file'] = 'The home page image was successfully uploaded.';
        }
    } else {
    //  $result['image_file'] = 'There was an error uploading the home page image. Please try again.';
    //  error_log('File ' . $file_data['profilebackground']['name'] . ' not uploaded.');
    }

    return $result;
}

public function getmainData($username) {
    $albums_json_file = $this->site_path . DIRECTORY_SEPARATOR . 'users' . DIRECTORY_SEPARATOR . $username . DIRECTORY_SEPARATOR . 'main.json';//$this->json_path . DIRECTORY_SEPARATOR . $username . '_'. 'main.json';
    try {
        $handle = @fopen($albums_json_file, "rb");
        if (!empty($handle)) {
            $albums_json_file_contents = fread($handle, filesize($albums_json_file));
            fclose($handle);
        }

        if (!empty($albums_json_file_contents)) {
            $albums_json_file_contents = json_decode($albums_json_file_contents);
        } else {
            $albums_json_file_contents = array();
        }

        if (!empty($albums_json_file_contents->main[0])) {
            return $albums_json_file_contents->main[0];
        } else {
            return $albums_json_file_contents;
        }
    } catch (Exception $e) {
        error_log($e->getMessage());
        $albums_json_file_contents = array();
        return $albums_json_file_contents;
    }
}
iSITKiosk
  • 59
  • 1
  • 6
  • Would it be possible to check that main.php is filled out? Or am I not understanding correctly. Do you mean it's possible to go directly to second.php without first hitting main.php? – bmcculley Mar 02 '16 at 21:44
  • you could set a session value after you set the JSON-flle – osanger Mar 02 '16 at 21:45
  • The two php files are two separate links. So the user can press on the "second" button on my website and go to that page. They can go to the second page without going to the main one. That's the problem. I need the second page to get the contents of the main.json. And, if empty, it will redirect to the main page. – iSITKiosk Mar 02 '16 at 21:51
  • Oh ok, I see. In that case, you could try what @dr_debug suggested or if your application is using a database you could try storing it in there. If the user hits the second.php check either of those and if it's not set send them back to main.php. – bmcculley Mar 02 '16 at 21:57

1 Answers1

0

Thanks for all of your help. I figured it out and it was really easy. I called the php that creates the main.json file and it retrieved data. Since it was empty it redirected fine. I'm not sure why it worked in the "else" part and not the "if" part but hey. It works.

require_once 'inc/main.class.php';
$mainevent = new mainevent();
$main_data = $mainevent->getmainData($user->username);
    if (!empty($main_data))
    {

        } else {
             redirect_to("main1.php");
        }
iSITKiosk
  • 59
  • 1
  • 6