blink359 Posted April 24, 2009 Share Posted April 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/ Share on other sites More sharing options...
jackpf Posted April 24, 2009 Share Posted April 24, 2009 You mean you want to redirect the user to a site when they've submitted? In that case, you can just use header('Location: http://www.site.com'); to do so. You can just put it at the end of your form process. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818322 Share on other sites More sharing options...
blink359 Posted April 24, 2009 Author Share Posted April 24, 2009 Where exactly sorry im new to PhP Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818340 Share on other sites More sharing options...
jackpf Posted April 24, 2009 Share Posted April 24, 2009 if (isset($_POST['Submit'])) You see that? At the end of that block, so something like this: if (isset($_POST['Submit'])) { //do all your stuff here header('Location: http://www.site.com'); } Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818344 Share on other sites More sharing options...
jonsjava Posted April 24, 2009 Share Posted April 24, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818385 Share on other sites More sharing options...
jackpf Posted April 24, 2009 Share Posted April 24, 2009 I generally like to think that the poster can do at least something for themselves, but yeah...there you go. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818430 Share on other sites More sharing options...
jonsjava Posted April 24, 2009 Share Posted April 24, 2009 I was the one who helped him yesterday, and I got busy, so I couldn't help him with the redirect at the same time. Decided to finish what I had started yesterday. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818449 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 Anybody else here a fan of a javascript redirect? instead of using headers just insert this where you want to redirect <script language="javascript"> window.location="index.php"; </script> Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818452 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818453 Share on other sites More sharing options...
mattal999 Posted April 24, 2009 Share Posted April 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818455 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818457 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818461 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 Actually there is another method metatags this allows u to display stuff, before redirecting. built right into html. no javascript, no headers required. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818467 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818468 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 I found the post with the metatag example. its a small redirect script that shows ya can redirect without javascript or header, and still toss some content into the browser Redirect with Ad Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818472 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 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 =\ Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818476 Share on other sites More sharing options...
mikesta707 Posted April 24, 2009 Share Posted April 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818478 Share on other sites More sharing options...
jackpf Posted April 24, 2009 Share Posted April 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155513-redirect-when-submiting/#findComment-818656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.