in need of simple php code.

Status
Not open for further replies.

DeJay

Active Member
68
2011
14
0
[SOLVED]in need of simple php code.

I need just a simple php code, its probly only a few lines of code.

anyways, here's what i need.

i need a php script in which i visit and it will INCREASE or DECREASE 1 number.

example URL would be.

site.com/example.php?type=inc
^^ that would increase my number by one

site.com/example.php?type=dec
^^ that would decrease my number by one.

i would prefer it be a POST instead of a GET request so this cannot be abused as much.

by default the number would be 0 of course.
i would like to be able to use this file so i can use it on a webpage to display the number.

so i could do something like

The number is: <? include(example.php) ?>

and would would display something like

The number is: 9

hopefully you understand what im saying.
 
Last edited:
6 comments
Im sure i could do this if you explain better.
Something like this?

<?php
$load
= file_get_contents("load.txt");
$load = $load + 1;
//$load = $load - 1; comment out above and uncomment this to subtract
$opentxt = fopen("load.txt", "w");
fwrite($opentxt, $load);
fclose($opentxt);

print
$load;

?>


 
you have the right idea, only i want it to do both in the same file.

here is what i mean.

if i open the link

Code:
http://www.site.com/example.php?type=increase

it would do

Code:
<?php 
$load = file_get_contents("load.txt"); 
$load = $load + 1; 
$opentxt = fopen("load.txt", "w"); 
fwrite($opentxt, $load); 
fclose($opentxt); 

print $load; 

?>

and if i done

Code:
http://www.site.com/example.php?type=decrease

it would do

Code:
<?php 
$load = file_get_contents("load.txt"); 
$load = $load - 1; 
$opentxt = fopen("load.txt", "w"); 
fwrite($opentxt, $load); 
fclose($opentxt); 

print $load; 

?>

but i want to be able to tell the .php file whether or not to increase or decrease the number.

im going to bed now, but i think i know enough php i might be able to finish the rest, but if someone that's better in php gets to it before me it would be greatly appreciated.

i think the rest that is needed to do what i want to do is just simple IF statements.
 
PHP:
<?php

/**
 * @author Somik Khan
 * @copyright 2011
 */

// Enter the name of the file which the count is stored to
$filename = "counter.txt";

// Get action from $_GET['type'] or $_POST['type'] variable
$action = $_POST['type'];




// Load the file into $visit file
$visit = file_get_contents($filename);
// Remove garbase data and only keep the integer value of visit
$visit = intval($visit);


if($action == "increase"){
    $visit++;
    file_put_contents($filename,$visit);
}
elseif($action == "decrease"){
    $visit--;
    file_put_contents($filename,$visit);
} 

echo $visit; 

?>


Usage:
Code:
To increase: 
Go to: http://www.site.com/example.php?type=increase
OR post "type" with value "increase" to example.php

To decrease: 
Go to: http://www.site.com/example.php?type=decrease
OR post "type" with value "decrease" to example.php

To display, just add:
include("example.php");
 
Status
Not open for further replies.
Back
Top