Jump to content

Redirecting from PHP code


poleman

Recommended Posts

Hi there,

 

I'm trying to redirect to a url once someone has entered their details into a form.

 

current code is :

 

<?php

$con = mysql_connect

("*********","********","******");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("********", $con);$sql="INSERT INTO phpbb_users(username,

user_email)

VALUES

('$_POST[name]','$_POST[useremail]')";if (!mysql_query

($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";mysql_close($con)

?>

 

 

I kn ow I've got to change the '1 record added' part of the code but not sure what to put.

Any text i put there will just bring it up int he browser.

So how do i get it to redirect to an entirely different url?

 

Hope someone can help

 

Thanks!!

 

Rich

Link to comment
https://forums.phpfreaks.com/topic/37645-redirecting-from-php-code/
Share on other sites

<?php
$con = mysql_connect 
("*********","********","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("********", $con);$sql="INSERT INTO phpbb_users(username,
user_email)
VALUES
('$_POST[name]','$_POST[useremail]')";if (!mysql_query 
($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con);
header('Location: whatever.php');
?>

 

Just use header('Location: whatever.php'); but it has to be used before anything hits the browser.

what do you mean, it has to be used before ianything hits the browser?

 

if i type in mysite name home page - www.jsay.co.uk.php,  will that work?

 

My site is in html, not php.

 

Also, now I get this error message when trying to sign up to the newsletter: Error: Duplicate entry '0' for key 1

 

I don't think I've changed anything on the php file so why is it showing this? How can I  fix it?

 

Thanks!!

The header has to be sent before any HTML or anything is displayed in the browser.  Everything between <?php and ?> tags is done on the server end, not on the client end.

 

You can't type in "www.jsay.co.uk.php" but you can type "http://www.jsay.co.uk/" ... it's just a path to something, doesn't have to be a website or an individual file ... you choice.

 

I believe that error is because you're duplicating content in your database ... could be wrong though.

I used header('Location: whatever.php') when redirecting to pages..However the limitation on using this is that you should declare it on the first line of your code.. That would not be cool..

 

I got a solution:

declare this tag on first line of your code..

<?php ob_start(); ?>

Doing so..you could use header('Location: whatever.php') Function on whatever part of your code.

 

FOR EXAMPLE :

 

<?

php ob_start();

......MY CODE..

if($numrows != 0) {

header("LOCATION: http://www.yahoo.com");

}

else

{

header("LOCATION: http://www.google.com");

}

?>

Hope i helped you.. <?php ob_start(); ?> is used to cache header info.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.