Jump to content

Recommended Posts

Hey all i have script and when they submit through the vote button i want it to take them to a site can some one show me the code to do it here is my php script

<?php
$unixts = date("U");
$unix_12hours = date("U") - (12*60*60);
$dbhost = "127.0.0.1";       //database host goes here
$dbuser = "your_user";         //database user goes here
$dbpass = "your_password";  //database password goes here
$dbname = "world";       //database name goes here

mysql_connect($dbhost, $dbuser, dbpass);
mysql_select_db($dbname);

$user = mysql_real_escape_string($_POST['user']);

$success = true;
$problemMessage = "Cannot connect to mysql server";
if (isset($_POST['Submit']))
{
if(!$user);
{
	$problemMessage .= "Please enter your account name <br />";
	$success = false;
}
if ($success)
{
	$sql = "SELECT COUNT(*) as `total_users` FROM `accounts` WHERE `lastvote` <= $unix_12hours AND `username` = $user LIMIT 1;";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	if ($row['total_users'] == 1){
		echo ("Thanks for voting!");
		mysql_query("INSERT INTO mailbox_insert_queue VALUES('$user','$user','Thanks for voting','Thanks for voting for our server please take this token as a thank you from our staff
      Thanks from the Sinister WoW team','61','0','1234500','1')"); //1234500 is the id for our token
		mysql_query("UPDATE `users` SET `last_voted` = $unixts WHERE `username` = '$user' LIMIT 1;");
	}
	else{
		$problemMessage = "You have voted in the past 12 hours";
	}
}
}
?>
<html>
<head>
<title>Voting Page</title>
</head>
<body>
<form>
Username: <input name="user" type="text"/><br>
<input name="Submit" type="submit" value="Vote" />
</form>
</body>
</html>

if you can add it into this code where it should go that would be great just make it go to google.com for now thanks

Link to comment
https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/
Share on other sites

here you go

<?php
$unixts = date("U");
$unix_12hours = date("U") - (12*60*60);
$dbhost = "127.0.0.1";       //database host goes here
$dbuser = "your_user";         //database user goes here
$dbpass = "your_password";  //database password goes here
$dbname = "world";       //database name goes here
mysql_connect($dbhost, $dbuser, dbpass);
mysql_select_db($dbname);
$user = mysql_real_escape_string($_POST['user']);
$success = true;
$problemMessage = "Cannot connect to mysql server";
if (isset($_POST['Submit']))
{
   if(!$user);
   {
      $problemMessage .= "Please enter your account name <br />";
      $success = false;
   }
   if ($success)
   {
      $sql = "SELECT COUNT(*) as `total_users` FROM `accounts` WHERE `lastvote` <= $unix_12hours AND `username` = $user LIMIT 1;";
      $result = mysql_query($sql);
      $row = mysql_fetch_assoc($result);
      if ($row['total_users'] == 1){
         mysql_query("INSERT INTO mailbox_insert_queue VALUES('$user','$user','Thanks for voting','Thanks for voting for our server please take this token as a thank you from our staff
      Thanks from the Sinister WoW team','61','0','1234500','1')"); //1234500 is the id for our token
         mysql_query("UPDATE `users` SET `last_voted` = $unixts WHERE `username` = '$user' LIMIT 1;");
         header('Location: http://www.site.com'); /* <----------- MODIFY THIS URL!!!!!!!!!!!!!!!! */
         exit();
      }
      else{
         $problemMessage = "You have voted in the past 12 hours";
      }
   }
}
?>
<html>
<head>
<title>Voting Page</title>
</head>
<body>
<form>
Username: <input name="user" type="text"/><br>
<input name="Submit" type="submit" value="Vote" />
</form>
</body>
</html>

Anybody else here a fan of a javascript redirect? instead of using headers just insert this where you want to redirect

 

Nope. I am a fan of coding it right and using the header. Due to the simple fact JS can be disabled/turned off.

 

And JavaScript's window.location has a small delay which irritates me :P

Anybody else here a fan of a javascript redirect? instead of using headers just insert this where you want to redirect

 

Nope. I am a fan of coding it right and using the header. Due to the simple fact JS can be disabled/turned off.

 

is it actually better practice to use headers? I understand that Javacsript can be turned off, but you can't do any output before hand, so If I wanted to redirect if someone logged in, but output something if they failed I wouldn't be able to do that with headers would I? Or can I?

is it actually better practice to use headers? I understand that Javacsript can be turned off, but you can't do any output before hand, so If I wanted to redirect if someone logged in, but output something if they failed I wouldn't be able to do that with headers would I? Or can I?

 

Nope. I am a fan of coding it right and using the header.

 

If you code right, not just echoing output as you go instead storing them in a variable(s) and then outputting those variables at the end you will be fine. It is not good practice to just echo stuff out through out the script. Execute the script first then output the items generated by the script.

is it actually better practice to use headers? I understand that Javacsript can be turned off, but you can't do any output before hand, so If I wanted to redirect if someone logged in, but output something if they failed I wouldn't be able to do that with headers would I? Or can I?

 

Nope. I am a fan of coding it right and using the header.

 

If you code right, not just echoing output as you go instead storing them in a variable(s) and then outputting those variables at the end you will be fine. It is not good practice to just echo stuff out through out the script. Execute the script first then output the items generated by the script.

 

Ahh I was entirely unaware of that.. thank you very much! But can you explain to me why it is bad practice to just echo stuff, instead of storing what you are echoing into variables, and output the stuff later? Is it just bad practice, or do certain problems arise from doing that?

Ahh I was entirely unaware of that.. thank you very much! But can you explain to me why it is bad practice to just echo stuff, instead of storing what you are echoing into variables, and output the stuff later? Is it just bad practice, or do certain problems arise from doing that?

 

Ultimately the header issue in it's self is a good reason. The other options is that you do not have control of the data in a sense. Also it makes it a pain if you want to implement "templating". If you store your output in variables you can easily template your system, where as if they are all in echos, you now have to re-do your logic/go back through and put all echo's into variables to be echo'd out.

 

Doing it this way makes it easier for templating, modifying down the road and solves the issues of header errors, session_start errors and other items. There is no real reason to just echo items out while it is processing. Also think that 1 echo vs 100 echo's will be much faster and less processing time. So aside from the errors it improves efficiency of the script. Process the script where it needs to process, display data where it needs to be displayed. Simple as that.

 

If you want more examples, I would suggest creating a thread in the Misc section to get examples and other's explanation.

 

Sorry to the OP for thread hijacking =\

Ahh I was entirely unaware of that.. thank you very much! But can you explain to me why it is bad practice to just echo stuff, instead of storing what you are echoing into variables, and output the stuff later? Is it just bad practice, or do certain problems arise from doing that?

 

Ultimately the header issue in it's self is a good reason. The other options is that you do not have control of the data in a sense. Also it makes it a pain if you want to implement "templating". If you store your output in variables you can easily template your system, where as if they are all in echos, you now have to re-do your logic/go back through and put all echo's into variables to be echo'd out.

 

Doing it this way makes it easier for templating, modifying down the road and solves the issues of header errors, session_start errors and other items. There is no real reason to just echo items out while it is processing. Also think that 1 echo vs 100 echo's will be much faster and less processing time. So aside from the errors it improves efficiency of the script. Process the script where it needs to process, display data where it needs to be displayed. Simple as that.

 

If you want more examples, I would suggest creating a thread in the Misc section to get examples and other's explanation.

 

Sorry to the OP for thread hijacking =\

 

Ahh that makes a lot of sense. Thank you very much for taking your time to explain that to me. I think you just changed the way I code slightly haha. Oh and OP im sorry too for hijacking.

You can use output buffering to send html to the client before sending a header (although the html wouldn't be seen).

 

And yeah, before anyone says "ouput buffering is slow/bad" It's not neccessarily. Output buffering can actually speed up html rendering by the browser, as it receives it all in one go.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.