SN1P3R_85 Posted September 2, 2008 Share Posted September 2, 2008 I honestly dont' really understand the header() funciton, all i know is that i've used it to redirect pages, and its worked some of the times. I've read that if there is any output before you execute the header(), that it wont' work. I am trying to redirect to another page after a use inputs some information in an html form. I store a session previously that contains the url. If you have any questions about my code, please just ask. I've been struggling with this for around a day, and i can't figure out whats wrong. Here is the code: <html> <head> <link rel="stylesheet" type="text/css" href="/css/standard.css"> </head> <body> <table class="lnk_bar" border="1" width="100%"> <tr> <td width="20%"><b><a href="/index.php">home</a></b></td> <td width="20%"><b><a href="/calc.php">calculator</a></b></td> <td width="20%"><b><a href="/cobolt.mp3">song</a></b></td> <td width="20%"><b><a href="/forums/forum_menu.php">forum</a></b></td> <td width="20%"><b><a href="/register.php">register</a></b></td> </tr> </table> <h4 class="forum_in">forum input</h4> <form class="one" action="forum_process.php" method="post"> //here is the user input <textarea rows="6" cols="40" name="forum"> </textarea> <input type="submit" value="submit" /> </form> <?php include( '../SQL_PASS.inc' ); //mysql login information include( '../user.inc' ); //user validation script if (isset($_POST['forum']) && strlen($_POST['forum'])>0) //if there is text in the input, it will execute the code { $url = $_SESSION['return_url']; //a session i saved on the previous page, just contains the url $msg = $_POST['forum']; if (!$con) { die('Cannot connect: ' . mysql_error()); } if (!$db_select) { die('Cannot select database: ' . mysql_error()); } $username = $user->Username; //just a pointer variable that contains the username $insert = mysql_query("INSERT INTO `test_forum` (`User_info`, `User_msg`) VALUES ('$username', '$msg')"); if ($insert) //here is where it should redirect. It executes fine, and runs the query, but just stays on the same page { mysql_close($con); //first i close my sql connection header("Location: $url"); //the header should redirect to $url } else { die('post failed: ' . mysql_error()); } } else { echo "field are not filled"; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 2, 2008 Share Posted September 2, 2008 More than likely do it like this: <?php if (isset($_POST['forum']) && strlen($_POST['forum'])>0) //if there is text in the input, it will execute the code { $url = $_SESSION['return_url']; //a session i saved on the previous page, just contains the url $msg = $_POST['forum']; if (!$con) { die('Cannot connect: ' . mysql_error()); } if (!$db_select) { die('Cannot select database: ' . mysql_error()); } $username = $user->Username; //just a pointer variable that contains the username $insert = mysql_query("INSERT INTO `test_forum` (`User_info`, `User_msg`) VALUES ('$username', '$msg')"); if ($insert) //here is where it should redirect. It executes fine, and runs the query, but just stays on the same page Sad { mysql_close($con); //first i close my sql connection header("Location: $url"); //the header should redirect to $url } else { die('post failed: ' . mysql_error()); } } else { echo "field are not filled"; } ?> <html> <head> <link rel="stylesheet" type="text/css" href="/css/standard.css"> </head> <body> <table class="lnk_bar" border="1" width="100%"> <tr> <td width="20%"><a href="/index.php">home[/url]</td> <td width="20%"><a href="/calc.php">calculator[/url]</td> <td width="20%"><a href="/cobolt.mp3">song[/url]</td> <td width="20%"><a href="/forums/forum_menu.php">forum[/url]</td> <td width="20%"><a href="/register.php">register[/url]</td> </tr> </table> <h4 class="forum_in">forum input</h4> <form class="one" action="forum_process.php" method="post"> //here is the user input <textarea rows="6" cols="40" name="forum"> </textarea> <input type="submit" value="submit" /> </form> <?php include( '../SQL_PASS.inc' ); //mysql login information include( '../user.inc' ); //user validation script ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
dubc07 Posted September 2, 2008 Share Posted September 2, 2008 use this instead echo '<meta content="0; URL=../login.php" http-equiv="Refresh" />'; so it would be if ($insert) //here is where it should redirect. It executes fine, and runs the query, but just stays on the same page Sad { mysql_close($con); //first i close my sql connection echo '<meta content="0; URL=../$url" http-equiv="Refresh" />'; //the header should redirect to $url You can change the 0 to a higher number to allow more time for redirect. ENJOY Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 hmm, i didn't think of that. So all u did was switch the php and html right? you didn't change anything about the code itself? from what i see it doesn't look like it, but its easy to miss it sometimes. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 2, 2008 Share Posted September 2, 2008 the meta version doesn't always work... Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 hmm, meta looks pretty easy, i'll mess around with these two suggestions, thanks guys Quote Link to comment Share on other sites More sharing options...
dubc07 Posted September 2, 2008 Share Posted September 2, 2008 <?php <?php if (isset($_POST['forum']) && strlen($_POST['forum'])>0) //if there is text in the input, it will execute the code { $url = $_SESSION['return_url']; //a session i saved on the previous page, just contains the url $msg = $_POST['forum']; if (!$con) { die('Cannot connect: ' . mysql_error()); } if (!$db_select) { die('Cannot select database: ' . mysql_error()); } $username = $user->Username; //just a pointer variable that contains the username $insert = mysql_query("INSERT INTO `test_forum` (`User_info`, `User_msg`) VALUES ('$username', '$msg')"); if ($insert) //here is where it should redirect. It executes fine, and runs the query, but just stays on the same page Sad { mysql_close($con); //first i close my sql connection echo '<meta content="0; URL=../"'.$url.'".php" http-equiv="Refresh" />'; //the header should redirect to $url } else { die('post failed: ' . mysql_error()); } } else { echo "field are not filled"; } ?> <html> <head> <link rel="stylesheet" type="text/css" href="/css/standard.css"> </head> <body> <table class="lnk_bar" border="1" width="100%"> <tr> <td width="20%"><a href="/index.php">home[/url]</td> <td width="20%"><a href="/calc.php">calculator[/url]</td> <td width="20%"><a href="/cobolt.mp3">song[/url]</td> <td width="20%"><a href="/forums/forum_menu.php">forum[/url]</td> <td width="20%"><a href="/register.php">register[/url]</td> </tr> </table> <h4 class="forum_in">forum input</h4> <form class="one" action="forum_process.php" method="post"> //here is the user input <textarea rows="6" cols="40" name="forum"> </textarea> <input type="submit" value="submit" /> </form> <?php include( '../SQL_PASS.inc' ); //mysql login information include( '../user.inc' ); //user validation script ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 im sorry, im new to php. What is this double tag thing? lol Quote Link to comment Share on other sites More sharing options...
dubc07 Posted September 2, 2008 Share Posted September 2, 2008 here's the proper way SORRY for the mix up echo '<meta content="0; URL=../"'.$url.'" http-equiv="Refresh" />'; Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 2, 2008 Share Posted September 2, 2008 im sorry, im new to php. What is this double tag thing? lol The double <?php shouldn't be there, you can remove one of them. Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 ya, i figured it out after a while. I used the meta thing, and it redirects me, but it redirects me to the main page. Since i set the session, im guessing this is one of my other errors. This is the $url variable: $url = $_SESSION['return_url']; $_SESSION['return_url'] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 so, do any of you guys see anything wrong with the session? Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 could you explain the meta thing you wrote. echo '<meta http-equiv="Refresh" content="3;url=../"'.$url.'" />'; I understand what http-equiv does, i understand what the content does, but why did you move up one directory before inserting the url? also, don't you have to end the quote that started with content? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 2, 2008 Share Posted September 2, 2008 i wouldn't use the meta if i were you... Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 well, what do you recommend? so far i've posted this and gotten little help from anyone. I'm willing to do whatever works, but i want to understand what it does. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 2, 2008 Share Posted September 2, 2008 listen to Little Guy, his post above. Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 ya, little guys's code has my includes at the bottom, so i can't even connect to my database using that. Quote Link to comment Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 w/e, fuck you guys, all i asked for was alittle help Quote Link to comment 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.