Jump to content

[SOLVED] help with header()


SN1P3R_85

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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'];                                                                                                                                                                                       

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.