• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple chat facility - if file exists not working

#1
We are trying to build a simple chat facility without using a database & have got the basic facility to work between 2 uses.

However, we want to first search to see if there is already a previous chat session in existence, but cant get that to work.

This is what we have tried;

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Chat extends CI_Controller
{

public function 
index()
    {

        if(
file_exists("C:xampp/htdocs/application/views/john/chris/chat"))  // Also tried if(is_dir(
        
{
        
redirect('chris/john/chat');  // Also tried $this->load->view('chris/john/chat');
        
}
        else  
// This is always rejected
        


If the file does not exist then the Controller Files & View Files are created. That part works OK but needs further work.

Can anybody guide us as to what we are doing wrong?
Reply

#2
It wont work because the whole application folder is using an .htaccess file
to stop viewing it. Look at the ,htaccess file.

What you would need to do is move it all to the root folder were it is writable.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(06-22-2020, 03:57 AM)InsiteFX Wrote: It wont work because the whole application folder is using an .htaccess file
to stop viewing it. Look at the ,htaccess file.

What you would need to do is move it all to the root folder were it is writable.

Thanks InsiteFX. Obviously I need to learn more about .htaccess files. I have found 2.

C:xampp/htdocs/application/.htaccess
PHP Code:
<IfModule authz_core_module>
    Require 
all denied
</IfModule>
<
IfModule !authz_core_module>
    
Deny from all
</IfModule

C:xampp/htdocs/.htaccess
PHP Code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond 
$!^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ /index.php?/$[L]
</
IfModule>
Header add Access-Control-Allow-Origin "*" 

I assume the root folder you speak of, is C:xampp/htdocs

I need to keep the chat files separate from the many base files Ive created for the website proper.

Is it wise to create chatterbox folders like C:xampp/htdocs/application/views/chatterbox/john/chris/chat & C:xampp/htdocs/application/controllers/Chatterbox/John/Chris/Chat and place a .htaccess file in the Chatterbox folder?
Reply

#4
@christaliise: What exactly dosen't work? It dosen't find the file?

This are the correct way of building a path.
PHP Code:
APPPATH.'views'.DIRECTORY_SEPARATOR.'john'.DIRECTORY_SEPARATOR.'chris'.DIRECTORY_SEPARATOR.'chat'

redirect will however not work, unless you have a controller that's named chris.
Reply

#5
(06-22-2020, 11:02 AM)jreklund Wrote: @christaliise: What exactly dosen't work? It dosen't find the file?

This are the correct way of building a path.
PHP Code:
APPPATH.'views'.DIRECTORY_SEPARATOR.'john'.DIRECTORY_SEPARATOR.'chris'.DIRECTORY_SEPARATOR.'chat'

redirect will however not work, unless you have a controller that's named chris.

Thanks jreklund.

I want first a search for the file, if it is found, then redirect, to 'chris/john/chat' (keep in mind it is chris that is logged in) page where the previous conversations will be exposed. That does not work as it redirects whether or not the file exists.

If the file is not found then it creates the 'chris/john/chat' & 'john/chris/chat' Controllers & view pages. The conversations can begin. That part I have working but it needs attention particularly in the variables for example.
PHP Code:
$lcuser $this->session->userdata('user_name');
$ucuser ucfirst("$lcuser"); 

redirect is not a problem. Yes, I have Controllers for all the pages, for example Controllers for "Chris/Chat", "John/Chat", "Chris/John/Chat, & John/Chris/Chat.

I dont understand your "correct way of building a path"? Can you explain?

I guess by "DIRECTORY_SEPARATOR" you mean forward slash? [/]. But I notice your full stops & single quotes ['.].

When doing a fwrite I use double quotes, spaces & full stops [" . ].
Reply

#6
This code:
APPPATH.'views'.DIRECTORY_SEPARATOR.'john'.DIRECTORY_SEPARATOR.'chris'.DIRECTORY_SEPARATOR.'chat';

Are the same as:
"C:\xampp\htdocs\application\views\john\chris\chat"

But it will work on all servers and operating systems. And if you ever move your folder, it will be found automatically, no need to change it manually every time. DIRECTORY_SEPARATOR will either generate / or \ depending of operating system. \ for Windows and / for Linux.

At the moment you are missing one / (actually should be \), in case you want to hard-code it.

Your code:
"C:xampp/htdocs/application/views/john/chris/chat"

Should be:
"C:\xampp\htdocs\application\views\john\chris\chat"
Reply

#7
If chat is the view file and not a folder you will need to add the .php extension also
when checking if the file exists.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.