Jump to content

Link Pressed


kirbyweb
 Share

Recommended Posts

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

You can do that in JavaScript for immediate results.For it to work with PHP, the link's href will have to be the address of a PHP script, and you could attach a GET request to the URL via query string.Or the link could trigger some JavaScript that posts a form. If the actual link makes a difference (like maybe you have several to choose from) the script could update a hidden form element based on some data in the link. Then your PHP script could check for that piece of data.If you want all this to happen without a page refresh, use AJAX.

Edited by Deirdre's Dad
Link to comment
Share on other sites

This is how you use the get method:http://www.php.net/manual/en/reserved.variables.get.phpThat's how you can pass information to PHP through the URL, the data shows up in PHP's $_GET array. In the URL for this thread you see a ? followed by "showtopic=" and a number, PHP has access to that number in $_GET['showtopic'].

Link to comment
Share on other sites

Well I have this code

<?phpinclude ('__________');//calculate money needed for next level of bank upgrade (php part of file)$need_money = 200;?><form method='POST'>Bank level: <?php echo $username['bank'];?> <br>Next level: <input type="submit" value="<?php echo number_format($need_money);?> money" name="upgradebank"></form><?php$upgradebank = $_POST['upgradebank'];//now we create another php part of this fileif (isset($upgradebank)) {//we check if user has enough money to upgrade bankif ($username['money'] < $need_money) {//we use die function to stop script execution and to show message what is wrongdie("You don't have enough money to upgrade Bank!");}//everything is OK, we update our user tablemysql_query("UPDATE `castle wars users` SET money=money-'$need_money', bank=bank+'1' WHERE id='$username[id]'") or die(mysql_error());}?>

I just want to make the form a link that refreshes the page, and everything else is the same except the form, and maybe something else.

Edited by kirbyweb
Link to comment
Share on other sites

First, when you do this:

$upgradebank = $_POST['upgradebank'];//now we create another php part of this fileif (isset($upgradebank)) {

That if statement will always be true, $upgradebank will always be set. You've set it above to the value in $_POST. If you want to check if the $_POST value was set you need to use that directly:if (isset($_POST['upgradebank']))You can use that to figure out if the form was submitted or not. If you know that the form was already submitted then you can modify whatever you want to modify. It may be easier if you use a variable to track if the form was submitted:

$form_submitted = isset($_POST['upgradebank']);

Then you can just use if($form_submitted) wherever you want to change something based on whether or not the form was submitted.If you want to make a link to same page, you can use the $_SERVER['PHP_SELF'] variable, e.g.:<a href="<?php echo $_SERVER['PHP_SELF']; ?>?t=<?php echo time(); ?>">Reload</a>The extra variable on the end there will make sure the page isn't cached when they click. If they click that link, the $_POST variable won't be set so you can still track whether or not they submitted the form. If you want to check if they clicked that link, you can check if $_GET['t'] is set.

Link to comment
Share on other sites

When you refresh a page, any GET parameters still apply because they are part of the query string. To get around that, you would need to do something like set a session variable on the preceeding page, to say 0, and check its value on the page in question. If it is zero, proceed as normal. Increment the session variable. Then if the page is reloaded, the session variable will be one greater and you will know it's been refreshed. The only way back in is through the page that set the session variable to 0 again. However, if you let someone into a page once, then refreshing will also be accompanied by a re-posting of form data, so it still doesn't count as navigating directly to the page.

Link to comment
Share on other sites

This way is easy. Say you have page1 that has a form and you want to allow it to be submitted to page2 only once. On page1, put this at the top:<?phpsession_start();$_SESSION["count"] = 0;?>Then whatever else you want. On page2, put this:<?phpsession_start();if($_SESSION["count"] && $_SESSION["count"] > 0) {//already been here, redirect:header("Location: somewhere.html");exit;}$_SESSION["count"]++;//form submit checks?>In order to visit the page, the user must first visit page one. On subsequent visits, even refreshes, the variable $_SESSION["count"] will fail the test. If you want the user only to be able to access this page once per visit, then you could simply add:if($_SESSION["count"]) {//already seen page 2, redirect:header("Location: somewhere.html");exit;}

Edited by chibineku
Link to comment
Share on other sites

Hm, I read a lot of stuff about POST and got a bit lost. However, the session variable thing works no matter how you end up on page 2. I may not be sure what you want to happen...

Link to comment
Share on other sites

You can probably use the referer header. The referer header gets set whenever you click a link, it tells the server where you came from. So this will be true if they clicked the link:

if (isset($_GET['t']) && !empty($_SERVER['HTTP_REFERER']))

If they refresh the page the referer should not be set, so that if statement should not be true for a refresh.

Link to comment
Share on other sites

What do you mean that code doesn't work? That line is just the start of an if statement that checks if $_GET['t'] is set and if $_SERVER['HTTP_REFERER'] is not empty. So whether or not it "works" depends on how you're trying to use it.

And also the number goes up over 500 when I am logged out.
If the maximum value is 500, then is the code where you update the number checking if the total will be over 500 before it updates the value?
Link to comment
Share on other sites

OK I fixed the link problem, the only problem now is this:

<?phpsession_start();$user = $_SESSION['username'];//open my database$connect = mysql_connect('_______', '_____', '___') or die('Couldn\'t connect!');mysql_select_db('_______castlewarslogin');$query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'");$username = mysql_fetch_array($query);$last = $username['lastupdate']; //timestamp from db$extraTime = time() - $last;$money = floor($extraTime / 60);if (($username['money']<500&&$username['bank']==0) || ($username['money']<600&&$username['bank']==1) || ($username['money']<700&&$username['bank']==2)) {if ($money > 0) {	//lets update users table	mysql_query("UPDATE `castle wars users` SET money=money+$money WHERE id= '$username[id]'") or die(mysql_error());}echo 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.';}elseecho 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.';	//lets update users table	mysql_query("UPDATE `castle wars users` SET lastupdate=lastupdate+$extraTime WHERE id= '$username[id]'") or die(mysql_error());?>

The number goes over 500 if the bank is at 1 it goes over 600 ect, how do I stop this.

Link to comment
Share on other sites

It's best to print everything out that the code is using so that you can see when it messes up and why. So print out every variable that you're using in the if statements, and then when it messes up you can look at what the values were and step through the code to figure out why those values made it do a certain thing. You can use error logging for this, I use this code on my pages to redirect all error messages to a file called error.log:error_reporting(E_ALL);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('log_errors', 1);ini_set('display_errors', 0);Then, you can use the error_log function to write whatever you want to the error log file. So you can use error_log to record the values that your variables have to help you debug the script, e.g.:error_log('money: ' . $money);Record any variable you use in an if statement to figure out why the if statements were either true or false.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×
×
  • Create New...