PHP - Flag file

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

PHP - Flag file

Post by alex4orly »

I am trying to set a file on the server that will serve as a toggle for a certain function. The code below used to work, but no it does not.
The Pop up message comes up OK from Confirm(), upon clicking OK, it gets into the if(r) but the PHP code is not executed.

Hope somebody can see what is wrong here...

Thanks

<script type= text/javascript>
function renameFile()
{
var r = confirm("Are you sure you want to do this?");
if(r)
{
<?php
$disable = "notyet.txt";

if (is_file($disable))
{
rename("notyet.txt","allowed.txt");
}
else
{
rename("allowed.txt","notyet.txt");
}
?>
}
}
</script>
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: PHP - Flag file

Post by WWBman »

I'm not an expert in Javascript nor PHP but I can't see how this would work.
I thought the PHP processor on the server processes the php code before passing it to the browser.
The browser would then process the Javascript.
So the php code testing if the file exists or not would be done irrespective of the javascript Confirm code.
I could be wrong here! :)

The other thing is that as it stands this idea only works if only one person uses the website as there is only one txt file.
Presumably you have taken this into account.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

I can see your point here.
The php code is executed irrespective...
This flag file is intended to allow or prevent members from a certain function.
The flag file is renamed by the administrator.
Any suggestion hkw else i can do that?
Is there a way to check for a file existance using javascript instead?
Thanks
User avatar
BaconFries
 
 
Posts: 5365
Joined: Thu Aug 16, 2007 7:32 pm

Re: PHP - Flag file

Post by BaconFries »

WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: PHP - Flag file

Post by WWBman »

I suppose another thing to take into account is that some users may disable Javascript!
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

Thanks guys, I see that I need to explain again what I need to do here:
The Administrator is in charge to allow or deny a certain function (membership renewal) for members during the year. That function checks for a file "notyet.txt".

The function below is intended at providing the Admin to toggle this file to another name, or back when the time comes.
It is not only that it needs to check for file existence, but also to rename it when required. Below is my latest attempt at it, the Confirm() action is ignored and the function is executed irrespective.

Any other way to do this?
Cheers

<script type= text/javascript>
function reneameFile()
{
if(confirm("Are you sure you want to do this?"))
{
<?php
if (file_exists("notyet1.txt"))
{
rename("notyet1.txt","allowed.txt");
echo "<script type='text/javascript'>alert('Restriction Removed!');</script>";
}
else
{
rename("allowed.txt","notyet1.txt");
echo "<script type='text/javascript'>alert('Restriction Applied!');</script>";
}
?>
}
}
</script>
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: PHP - Flag file

Post by WWBman »

As I said I don't think you can mix php and javascript this way.
Did you look at BaconFries' suggested websites for Ajax etc.?

BTW it should be <script type="text/javascript"> with double quotes.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

Hello again,

I never played with Ajax. He is pointing me to how to check for a file, but do I have an Ajax option also to rename the file like with PHP?
I have never used Ajax and don't know how to embed this into my HTML page.

Any help will be appreciated

Thanks

P.S - where are you in the world answering me at this time? I am in Melbourne, Australia
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: PHP - Flag file

Post by WWBman »

I am in the UK.
I think it can all be done using Ajax but unfortunately I also have never used Ajax in earnest.
User avatar
BaconFries
 
 
Posts: 5365
Joined: Thu Aug 16, 2007 7:32 pm

Re: PHP - Flag file

Post by BaconFries »

He is pointing me to how to check for a file, but do I have an Ajax option also to rename the file like with PHP?
I/He will answer this himself even if the time is UK 23:35 pm. You can use/insert any code including ajax into the programme using either the (mentioned frequently in the forum) HTML Object or in the Page HTML. With this It is generally understood though that if you wish is use PHP/javascript/Ajax etc then that you yourself have a understanding (or some) on how to use.

Now in no way am I saying that you cannot or are not allowed to ask for help or assistance with this and others are free to help if they wish to but if you do not get the answer you so wish then you will have to do research using "Google". One of my personal site reference I would recommend is http://www.stackoverflow.com/ signup required when posting try to give as much information as possible to get the best answer and always give a thanks to all that helped resolve your query.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

Thanks guys,

I always appreciate any help and don't feel anybody owes me something.
I am a member on that stackoverflow site and posed a query there already

Cheers and good night UK
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

Hello folks,

Well, while you were a sleep, I resolved my problem and wish to share it with you all. It is made of two parts
The JavaScript function - is like this, if the Admin selects OK, it loads a PHP script:

<script type= text/javascript>
function doit()
{
if(confirm("Are you sure you want to do this?"))
{
window.location.href = "rename.php";
}
else
{
alert("Cancelled");
}
}
</script>

And here is the PHP script
<?php
if (file_exists("notyet.txt"))
{
rename("notyet.txt","allowed.txt"); // It does the renaming and goes back to the calling page after showing an Alert box
echo "<script type=\"text/javascript\">window.alert('Restriction Removed!'); window.location.href = '/dataviewer.php';</script>";
exit;
}
else
{
rename("allowed.txt","notyet.txt"); // It does the renaming and goes back to the calling page after showing an Alert box
echo "<script type=\"text/javascript\">window.alert('Restriction Applied!'); window.location.href = '/dataviewer.php';</script>";
exit;
}
?>

Thanks for your help, hope someone else can use this
Cheers and good night from Down under
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: PHP - Flag file

Post by WWBman »

Thanks, well done.
Interesting way to do it.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

Well WWBman,
It was your comment that PHP is executed on the server first before passing control to the browser that made me think up this idea
So, thank you!

And, you still didn't tell me where in the world you are...

Cheers
User avatar
BaconFries
 
 
Posts: 5365
Joined: Thu Aug 16, 2007 7:32 pm

Re: PHP - Flag file

Post by BaconFries »

@alex4orly
WWBman wrote: Thu May 11, 2017 10:38 pm I am in the UK.
I think it can all be done using Ajax but unfortunately I also have never used Ajax in earnest.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: PHP - Flag file

Post by alex4orly »

Thanks Baconfries,
Neither have I and this seems to work just fine as intended

Cheers
Post Reply