Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

PHP to create new .js file for each registered user

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PHP to create new .js file for each registered user

Postby vpetkovic » Tue Aug 05, 2014 8:02 am

Hello everybody,

This is my first post here and I am beginner in PHP world in terms of writing the code to serve my own purposes.

Well I am building a web app and basically it's a calendar which pulls information from .js file. Now I have thinking for the past couple of days how can I accomplish that each user that registers on the site manipulates its own .js file because information from .js file will be shown on calendar.

Let me tell you how it's currently set up:

1. JavaScript file with particular static name is called under the script that is placed on index.php and the data is displayed on the page itself.

So I would love to have is set it up like this:

1. Index page contains login form - Each registered/logged in user will have its own session
2. User registers and based on username/email new .js file is created out of a blank template and it is named based on user's username
3. user is then redirected to the calendar index which contains javascript that cals out that appropriate .js file based on the what user is logged in and displays data to the calendar

I am not sure if that is doable with PHP or not but that's my thinking how it can be done if it's doable. I am open for any kind of suggestions how all this can be put together and if you do have better ideas I would love to hear from you.
vpetkovic
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Aug 05, 2014 6:26 am

Re: PHP to create new .js file for each registered user

Postby landi » Tue Aug 05, 2014 8:28 am

Hi,

If I understand correctly, your idea it is to create a .js per user e.g. lucy.js and fred.js and you would include the appropriate file something like this:
Code: Select all
  <?php $sUser = 'fred'; // Get this from the session ?>
  <script type="text/javascript" src="calendars/<?=$sUser?>.js"></script>


The problem with this, is that any user could access any calendar, just by guessing the user name. You could make the names complicated to add some security.

A better way, would be to make the .js file dynamic.
1) Call it calendar.php instead of user.js

2) Include it like this:
Code: Select all
<script type="text/javascript" src="calendars/calendar.php"></script>


3) Inside calendar.php, use php to generate the unique javascript for the user e.g.
Code: Select all
// Remember, this is javascript or php in this file
var sUser = "fred"; // javascript
var sUser = "<?=$sUser?>"; // javascript with embedded php
function getMyCalendar() { // Javascript
  <?php
     print "return '$sValue'; "; // Outputs return 'value';
  ?>
} // Javascript


By using this method, you don't need a 'template', because the calendar.php file IS the template, and it is just filled in with the user's data.

Hope that helps.
-A
landi
New php-forum User
New php-forum User
 
Posts: 236
Joined: Thu Mar 15, 2012 3:59 pm

Re: PHP to create new .js file for each registered user

Postby vpetkovic » Tue Aug 05, 2014 10:06 am

Hello Landi,

Thanks for the reply. You understood me correctly and give me even better idea. I kinda didn't think it is possible to embed php into js the way you showed me but you took me to the right direction with your suggestion.

So now it got me thinking again, since that js file is generated on the fly I can also store data in database and make php to populate that .js with data from db, is that right?
vpetkovic
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Aug 05, 2014 6:26 am

Re: PHP to create new .js file for each registered user

Postby landi » Tue Aug 05, 2014 10:45 am

Correct. But you could also use php to populate the calendar WITHOUT using js, unless you need js for some other reason. E.g. something like ownCloud generates the calendar page directly from php. But - whatever works for you :)
- A
landi
New php-forum User
New php-forum User
 
Posts: 236
Joined: Thu Mar 15, 2012 3:59 pm

Re: PHP to create new .js file for each registered user

Postby vpetkovic » Wed Aug 13, 2014 4:57 am

Thank you for your help... i am figuring the code right now.
vpetkovic
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Aug 05, 2014 6:26 am


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron