couple of doubts regarding php & ajax.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

couple of doubts regarding php & ajax.

Post by claws »

1. i noticed few times that @ (at the rate) symbol is used in php. what is this symbol. and in which context it is used?

2. why does the code inside <script> tags doesnt work if introduced using ajax. i mean
say if the response of my ajax query is
<script>alert("I Love PHP");</script>
now. using dom if i introduce this response at some node of the page. its not going to alert. why??

3. i am builing a portal in which i have a field named "validity". this is the validity period of the user's data. i mean say if validity=30 days (which is default value iam using) after thirty days that user's data will be deleted automatically. daily its value should be reduced by 1.
but how to implement this.i cant daily run a script. i want to automate this task. how to do that.
i heard of something called CRON. how to implement it?
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Re: couple of doubts regarding php & ajax.

Post by webspider »

claws wrote:1. i noticed few times that @ (at the rate) symbol is used in php. what is this symbol. and in which context it is used?
@ is error suppression operator, can be used in front of any expression, that is, anything that has or generates a value.

Code: Select all

$a = @(100/0);
Without the @ operator this line will generate a divide by zero warning. With the operator included, the error is suppressed.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Re: couple of doubts regarding php & ajax.

Post by nathanr »

claws wrote:1. i noticed few times that @ (at the rate) symbol is used in php. what is this symbol. and in which context it is used?
error supression
claws wrote:2. why does the code inside <script> tags doesnt work if introduced using ajax. i mean
say if the response of my ajax query is
<script>alert("I Love PHP");</script>
now. using dom if i introduce this response at some node of the page. its not going to alert. why??
extract the scripts (using javascript) from the ajax return and add them to the head, then they'll run
claws wrote: 3. i am builing a portal in which i have a field named "validity". this is the validity period of the user's data. i mean say if validity=30 days (which is default value iam using) after thirty days that user's data will be deleted automatically. daily its value should be reduced by 1.
but how to implement this.i cant daily run a script. i want to automate this task. how to do that.
i heard of something called CRON. how to implement it?
cron = crontab, only on linux, normally found under /etc/crontab - you can edit it via vim/nano or such like - cron checks this file at regular intervals to see if there's anything it needs to run.
here's a line from mine:

Code: Select all

5  *    * * *   root    php -f /home/sites/domain.com/maildaemon.php
this runs a script called mailerdeamon via the command line every hour.

however in your case, you don't really need to decrement daily, all you need to do is know when the user signed up, then run a script every day that checks for expired users (signup date + 30 I'd presume) - and does the work. You could even add it to your index.php

if(!$clean_up_code_has_run_today) {
run_clean_up_code();
}

it means one visitor will take the weight of the clean up job, but thats only one visitor on one page call per day..

ps: why delete, why not just "deactivate"??
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post by aliasxneo »

You don't need a cron. Just pick a moderately trafficked web page and add code that decrements the values each day. Just keep track of the last time you performed the decrement operation and check if 24 hours has passed, if so decrement them all and update the time.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post by Inkyskin »

That would add unnecessary code to the page that would run EVERY time it's hit - a cron is much more effective, and doesn't add more code to a page that doesn't need it. It's also a much cleaner method of running the script. It also pretty much guarantees the script runs even if that page goes down or doesn't get hit.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post by aliasxneo »

Inkyskin wrote:That would add unnecessary code to the page that would run EVERY time it's hit - a cron is much more effective, and doesn't add more code to a page that doesn't need it. It's also a much cleaner method of running the script. It also pretty much guarantees the script runs even if that page goes down or doesn't get hit.
Well of course a cron would be the best option, but:
i cant daily run a script
I figured that meant he didn't want to run a cron which is why I suggested the alternative. And besides, you act like such a request would really slow a page down.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

Inkyskin wrote:That would add unnecessary code to the page that would run EVERY time it's hit
not EVERY time only ONCE.. you see you code in a check to see if the clean up codes run already in the past 24 hours or not..

again though.. why decrement. simply only allow selects to people in your db who have a subscription date within the last 30 days.. problem solved with no complicated scripts or deltions or such like.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

nathanr wrote:simply only allow selects to people in your db who have a subscription date within the last 30 days.. problem solved with no complicated scripts or deletions or such like.
Absolutely! You don't need to do anything at all until or unless the user attempts to login. When they do, check their status. Don't worry about their data remaining in the database--storage is cheap! If you get over 100 million users (you should be so lucky!) and you start worrying about disk space, you can run an admin script once a year to clean up the old records.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

I also suggest you to check expired date rather than cron or decrement and why don't you better disable a user rather than deleting the record of the user from the database.
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

Post by claws »

why don't you better disable a user rather than deleting the record of the user from the database.
yeah. i am not deleted. but just disabling.
Absolutely! You don't need to do anything at all until or unless the user attempts to login. When they do, check their status.
thats true. but this may not work in my case.
actually. their information(profiles,testimonials) are viewed publicly.
so its not "You don't need to do anything at all until or unless the user attempts to login."
so.. even the user doesnt login for say 30days or more. his profile should be automatically disabled by 30days.

yeah. i feel CRON is the better option.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

claws wrote:thats true. but this may not work in my case.
actually. their information(profiles,testimonials) are viewed publicly.
so its not "You don't need to do anything at all until or unless the user attempts to login."
so.. even the user doesnt login for say 30days or more. his profile should be automatically disabled by 30days.

yeah. i feel CRON is the better option.
I disagree. Your query that displays user information should always check for the date status. It's really very simple. Avoid making things complicated wherever possible.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

califdon wrote:
claws wrote:thats true. but this may not work in my case.
actually. their information(profiles,testimonials) are viewed publicly.
so its not "You don't need to do anything at all until or unless the user attempts to login."
so.. even the user doesnt login for say 30days or more. his profile should be automatically disabled by 30days.

yeah. i feel CRON is the better option.
I disagree. Your query that displays user information should always check for the date status. It's really very simple. Avoid making things complicated wherever possible.
In this case I would agree with califdon. If you are storing the last login date as a mysql timestamp then it is a simple WHERE clause to determine who is valid or not. No need for a separate cron daemon to do extremely simple processing.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Re: couple of doubts regarding php & ajax.

Post by CoderGoblin »

claws wrote:1. i noticed few times that @ (at the rate) symbol is used in php. what is this symbol. and in which context it is used?
As has already been noted this is used for error suppression but should be used with caution. Hiding errors is not a good solution, Defensive coding is. If you are not careful lack of error messages may produce annoyingly hard to trace errors elsewhere in your code.
chakhar86
Forum Commoner
Posts: 45
Joined: Mon Jun 05, 2006 1:36 am
Contact:

Post by chakhar86 »

equal to crontab on linux.... use "at" in windows
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

icron is a cron analogue on Windows. "at" is not as direct of an analogue.
Post Reply