Redirect PHP after script is run - php

I have a script that submits emails entered into a form to a database and I am wondering if there is a way to redirect the PHP script after the email has been submitted?
my script is -
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
please could someone give clear instructions as I am learning PHP still.

To take this redirect one step further. The header "location" should be used with a 302 response code instead of a classical 200 response code. Thios response code is 'global' to the HTTP response.
302 means 'redirect'. Most browser handle the location header even without the 302 response code, but you should set it (it could help for example ajax handling of your response).
header("Location: /foo.php",TRUE,302);
Now one step more. In fact a 303 response code (see-other) is a better redirect response code. It's called as well the redirect after post. This special redirect means for your browser that it's not really a redirect because the page he asked whas in the wrong place, but that it's really a redirect because after your POST action you need a new page result.
header("Location: /foo.php",TRUE,303);
Edit: as stated by thirtydot, a better usage is absolute url in the redirect
header("Location: http://www.example.com/foo.php",TRUE,303);
Remember that POST implies potential data changes (that GET request should'nt imply). A POST followed by a 303 is the right way to go, and will prevent your BACK button reposting the same request.

Use:
$url = 'http://your.redirect.url';
header('Location: ' . $url);

Try this:
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email) VALUES ('$_POST[email]'')";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }
mysql_close($con);
header('Location: http://www.example.com/');

This code taken from the header() manual page will redirect to another page on the same domain and in the same directory:
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;

header("Location http://www.example.com");
That will redirect the browser to example.com, note that you can only use this if output has not already been sent to the browser, e.g make sure you have echo, print or var_dump statements before you use header()

Add send header function
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}else{
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
mysql_close($con)
?>
Or send javascripts with code
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}else{
echo "<script>";
echo "window.location.href= '".$_SERVER["HTTP_REFERER"]."';";
echo "</script>";
}
mysql_close($con)
?>

Redirect in the controller.
header('Location: http://www.yoursite.com/new_page.html');

Related

How to resolve echo error:Error: Duplicate entry in home header

I keep getting this error displayed in a new page
Error: Duplicate entry 'company' for key 'cname'
how can i display this in a new header
Heres my code:
$con=mysqli_connect("localhost","root","password","mydb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO profile (cname, cobusiness, cphone,crep,cdescription)
VALUES
('$_POST[cname]','$_POST[cobusiness]','$_POST[cphone]','$_POST[crep]','$_POST[cdescription]')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
header("Location: home.php?error");
} else {
header("Location: home.php?success");
exit;
}
mysqli_close($con);
I have added a trigger in mysql to check for duplicate entries and just need to figure out how to prevent this duplicate entry error display in a new page by default.This way i can display it in a header on the current page like i have already done with the other headers in the code.
With valuable assistance from Jean-maxime Bouloc
This is the solution:
Insert.php
$sql = "INSERT INTO profile (cname, cobusiness, cphone,crep,cdescription)
VALUES
('$_POST[cname]','$_POST[cobusiness]','$_POST[cphone]','$_POST[crep]','$_POST[cdescription]')";
if (!mysqli_query($con,$sql)) {
header("Location: home.php?error");
}
else {
header("Location: home.php?success");
exit;
}
mysqli_close($con);

Multiple PHP "header(url)" in script, "Cannot modify header information" Error

Hey I have some script that is supposed to change the url of the page if it meets certain criteria, or somewhere else if it doesn't.
I'm getting this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/hermes/bosweb25c/b1401/ipg.website/process.php:7) in
/hermes/bosweb25c/b1401/ipg.website/process.php on line 53
Here's my PHP. I'm pretty sure it has to do with the if statement, but it was working fine on my WAMP Server before I uploaded to my web host.
<?php
session_start();
$con = mysql_connect('website', 'members_db_admin', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
// make members the current db
$db_selected = mysql_select_db('members_db', $con);
if (!$db_selected) {
die ('Can\'t use members database : ' . mysql_error());
}
$hash_password = md5($_POST['password']);
$email = $_POST['email'];
$result = mysql_query("SELECT email,password FROM `members` WHERE email = '$email'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
}
$row = mysql_fetch_row($result);
if ($row[0] != $email && $hash_password != $row[1])
{
$query = "INSERT INTO members (email, password)
VALUES('".$_POST['email']."','".$hash_password."')";
// Perform Query
$result2 = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result2) {
$message = 'Invalid query: ' . mysql_error() . "\n";
//$message .= 'Whole query: ' . $query;
die($message);
}
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$_SESSION['loggedin']="YES";
$url = "Location: /welcome.php";
header($url);
}
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$url = "Location: /checklogin.php";
header($url);
?>
Your problem is echo 'Connected successfully<br />';. You can't print anything out before setting headers with a header() call. Make sure you don't have any echo calls before your header().
try ob_start(); after session_start();
If you use utf-8 as encoding for this file try to save it without BOM which can cause such problems.

PHP form generates "Object not found" but still submits form data

I've got a simple php form for client info that when submitted, generates an object not found 404 error. The information I submitted is still being sent to the db successfully so I'm not sure why I'm seeing this error. Thanks in advance for the help.
<?php
$link=mysql_connect("localhost","root","");
$database='clientinformation';
if (!$link)
die('Failed to connect to Server'.mysql_error());
$db=mysql_select_db($database, $link);
session_start();
if(!$db)
die('Failed to select Data Base '.mysql_error());
if(isset($_GET['process']))
{
$query = "Insert INTO `client_reg` (ClientName, Address, CNICNumber, MobileNumber, TelephoneNumber, CompanyName, ClientStatus, RegisterDate) values('$_POST[ClientName]', '$_POST[Address]','$_POST[CNICno]','$_POST[Mobileno]', '$_POST[Telephoneno]', '$_POST[Companyname]', '$_POST[Clientstatus]', '$_POST[RegisteredDate]')";
//echo $query; exit;
$result = mysql_query($query) or die(mysql_error());
if(!$result)
{
$msg = "not Inserted";
}
else
{
$msg = "Inserted";
header("location:ClientList.php?m=".$msg);
}
}
?>
Does Clientlist.php exist on your server? Remember that UNIX servers are case sensitive. If it's clientlist.php on the drive, you'll get a 404
404 error - "404 Not Found" web page when a user attempts to follow a broken or dead link;
`ClientList.php` the file not found or mismatching in filename
You should follow this convention :
$_POST["name"])
Store value after submit form:
$clientName=$_POST['ClientName'];
$Address=$_POST['Address'];
$Mobileno=$_POST['Mobileno'];
$Telephoneno=$_POST['Telephoneno'];
$Companyname=$_POST['Companyname'];
$Clientstatus=$_POST['Clientstatus'];
$RegisteredDate=$_POST['RegisteredDate'];
Now use this value in your query,hope it will work.

header working on localhost but not working on a live server

wonder if anyone could help.
my problem is that the site works on localhost but when I ftp it to a live server the header() doesn't redirect them to (in this case) a thank you page, the data how ever is recorded in the database.
PHP code:
<?php
if (isset($_POST['submit'])) {
$connect_error = 'Sorry, Connection problems.';
mysql_connect('localhost', 'user', 'password') or die($connect_error);
mysql_select_db('email_logs') or die($connect_error);
$sql = "INSERT INTO client_data (name, email) VALUES ('" . mysql_real_escape_string($_POST['name']) . "', '" . mysql_real_escape_string($_POST['email']) . "')";
mysql_query($sql);
mysql_close();
header("Location: thankyou.php");
exit();
}
?>
your code is fine...if u want u can try defining relative paths like.../path/to/thankyou.php..watch what happens..if it still not running..then check by error_reporting(E_ALL);ini_set('display_errors', 1); and tell us what the actual error
This may help you.
ob_start();
You can add this at the top of thankyou.php
Try this at the top of the script
ob_start("ob_gzhandler");
Try with the below piece of code
fileName:fix.php
<?php
ob_start();
...
header('Location: page1.php');
...
ob_end_flush();
?>

PrestaShop Fatal error in function isLogged()

Well, im developing a CMS based form with a copy-center-system (this is the form), but i need the $cookie->id_customer.
But i get an error:
Fatal error: Call to a member function isLogged() on a non-object in /home/papelari/public_html/modules/mymodule/submit.php on line 13
<?php
/*
$con = mysql_connect("localhost","papelari","509494218");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("papelari_Veloso", $con); */
global $cookie;
if(!$cookie->isLogged())
{
echo 'Please login';
exit;
}
else
{
echo 'Hi, ' . $cookie->customer_firstname. ' ' .$cookie->customer_lastname . '<br/>';
echo 'We contact to you: '. $cookie->email;
}
/* $sql="INSERT INTO ps_copias (id_customer, file, cor, copias, pags, papel, gramagem, flag) VALUES('$cookie- >id_customer','$_POST[file]','$_POST[cor]','$_POST[copias]','$_POST[pags]','$_POST[papel]','$_POST[gramagem]', 0)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Enviado com sucesso!";
mysql_close($con); */
?>
Your problem is that $cookie is not defined. Paste this at the beginning of your script :
<?php
include("../config/config.inc.php");
$cookie = new Cookie("ps");
if ($cookie->isLogged()) {
// your code
}
You may have to adapt config.inc.php include path depending on your own script
I am not sure that the global $cookie is for.
Make sure that $cookie is defined by using var_dump($cookie); to make sure the variable is what you think it is (and contains the correct functions).
For prestashop 1.5, to check is user is logged or not :
Context::getContext()>customer>isLogged()

Categories