Header Location in php - php

<?php
require ('db_connect.php');
$Name = $_POST["name"]; $Email = $_POST["email"]; $Message = $_POST["message"];
if( isset($_POST["submit2"]) ) {
$insertString2 = "INSERT INTO Messages(Name,Email,Message)
VALUES('$Name','$Email','$Message')";
mysql_query($insertString2);
header("Location:register.php?msg=Successfully Send Message! You will get reply soon...");
}
?>
This is my code. The MySQL part is working. But this does not redirect to register.php. Why?

You may put a header call in an if-conditional preceded by other code, as long as you follow what the Manual recommends:
You can use output buffering ... by calling ob_start() and ob_end_flush() in your
script, or setting the output_buffering configuration directive on in your php.ini
or server configuration files.
If you wish to alter your PHP.INI file, you may turn output buffering on, as follows:
output_buffering = On
With output buffering turned on, this code structure should work:
Try this for your header call:
// setting variables here, then:
if (condition) {
// other code here, then:
$location = "http://yourdomain.whatever/register.php";
$encoded = urlencode("Successfully Send Message! You will get reply soon...");
header("Location: $location?msg=$encoded");
exit;
}
Note: when you use header() if you have any concern with maintaining backwards compatibility with http1.0, then you should provide a full url, including the protocol whether that is 'http', 'https' or something else. Also your query string contains characters that need to be urlencoded. After passing the value to be encoded to urlencode(), the string looks like this:
Successfully+Send+Message%21+You+will+get+reply+soon...
Part of the joy of using PHP is that it does nice things like automatically decoding the encoded url. So all you need do to display a message to the user is write something similar to the following code at register.php:
<?php echo htmlentities($_GET['msg']);
if you wish to decode the $_GET variable with JavaScript, just be aware that PHP and JavaScript do not urlencode spaces the same way, so you need to manually decode the $_GET variable's value yourself rather than fully relying on JavaScript's decodeURIComponent(). The following JavaScript will urldecode the value of "msg", including converting any '+' characters into spaces:
var str=location.search.substring(5);
str = decodeURIComponent( str );
str=str.replace(/\+/g, ' ');
alert(str); //Successfully Send Message! You will get reply soon...

"It is important that header() must
be called before any actual output is sent"--you may miss this point.

Related

passing variables from form to update but url doesnt shows the value for it

i'm passing variables through url to function but it not reflect anything
here is the code for url:
$result = file_get_contents('http://localhost/service/service.php?action=update_details&id=$id,&name=$name,&lname=$lname,&email=$email,&username=$username,&password=$password,&gender=$gender,&mobile=$mobile,&address=$address');
and function is on other php file. Here is code:
if($tag == 'update_details') {
$id = $_GET["id"];
$name=($_GET['name']);
$lname=($_GET['lname']);
$gender=($_GET['gender']);
$email=($_GET['email']);
$username=($_GET['username']);
$password=($_GET['password']);
$mobile=($_GET['mobile']);
$address=($_GET['address']);
if ->updateUser($id,$name,$lname,$username,$password,$gender,$email,$mobile,$address)) {
$app_info = "success";
exit (json_encode($app_info));
} else {
$error="not done";
exit(json_encode($error));
}
}
Remove comma from url:
$result = file_get_contents('http://localhost/service/service.php?action=update_details&id=$id&name=$name&lname=$lname&email=$email&username=$username&password=$password&gender=$gender&mobile=$mobile&address=$address');
First you must take in account all the advices you already got:
replace simple quotes by double quotes (as noticed by #Rasclatt), in order to have your embedded variables interpreded
suppress commas (as already by #Jayesh Chitroda) to get a normally built url
Then in addition you must take care of this PHP manual advice :
If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().
So if some of your variables may contain such characters (which is likely expected in your case) you must also urlencode() them.
Remember: don't urlencode() the whole URI, but each involved variable.
This way you will correctly invoke your service.php script, but there is yet another potential issue (also already noticed by #Rasclatt): if ->updateUser(...)){ has no sense.
Maybe it's a typo in your question? It's now up to you to examine this point.

Jquery POST and AJAX prepend double space to returned data

I am still struggling with this problem after about 2 weeks with no sign of a solution.
Any data that is returned by PHP using $.ajax or $.post always has two spaces added onto the returned data. I have trimmed the data being echoed in PHP to confirm it's not an issue with the server or my scripts.
e.g.
echo '{"id": "'.$myId.'"}';
Becomes:
' {"id": "'.$myId.'"}'
When viewing the returned data in inspector. This causes problems for my js scripts because they expect nothing returned when there is no error. Double space is returned which causes errors when there are actually none, which in turn stops other events from firing.
I am using Jquery 1.8.3.
Does anyone have any idea what is causing this extremely strange and annoying issue?
I am using NetBeans
I recall that this only started happening since I moved my app to a new server, but I don't see how that would have effected it in this way.
This may be helpful,
I think there is a whitespace in your script (may be from included files, but not sure). You can overcome this by clearing the output buffer, before you send the data to browser. The following code will demonstrate the idea.
<?php
ob_start();
echo ' '; // Possible whitespace (may be from included files)
----------
----------
if (YOUR_CHECK_FOR_AJAX_REQUEST) {
ob_end_clean();
$myId = 1;
echo '{"id": "'.$myId.'"}';
exit;
}
?>
There is chance that you've got whitespaces at the end of your included php files (to prevent it, skip ending ?> tag).
Also you should check if there is nothing in front of <?php and disable BOM in your UTF-8 files.
The data which is returned from the file would have spaces or you would have html tags. Remove all the tags, lines, spaces and at the end remove ?>
If you are returning data from test.php the file should look like as below
<?php
$myId = '1';
echo '{"id": "'.$myId.'"}';

php redirect and querystring

i have script
<?php
$to = $_GET["to"];
header("Location: $to");
?>
if i call script such
out.php?to=http://site.ru/page.php?param1=1&param2=2
in param $to be only http://site.ru/page.php?param1=1&
how to fix? i want that $to = http://site.ru/page.php?param1=1&param2=2
You can escape the URL at the site calling out.php:
Go to $to
& is a reserved character in an URI. When you access this URL, &param2=2
is interpreted as belonging to the current URL and not to the value of to.
If you want to transmit it literally, you have to encode it with %26:
http://site.ru/page.php?param1=1%26param2=2
Most programming languages provide a function to do so. (e.g. JavaScript, PHP). The best thing is to encode the whole URL.
$to must be urlencoded, but note that you giving a redirect script to anyone, so, any phisher can use it.
So, it would be better to store urls in the database and pass only an identifier.
try encoding the to URL in base64 and then in the example that u have shown decode it before you pass it to the header :)
urlencode it
urlencode($to)
I ran into the same problem before, this is what I did:
$arr=explode('?to=',$_SERVER['REQUEST_URI'],2);
$new_to=$arr[1];
Now you can use the $new_to variable.
Of course if you're using this for production environment, I would recommend encoding the url as the other answers advised. I was using it for testing curl script. getting the variable this way has lots of flaws, so be careful.
You can use a Function called "html_entity_decode"
Click Here for more information about this function
or use md5 function to encrypt the URL and then decrypt it when you put it into a varriable.
I hope this can help you

Problem with sending "SetCookie" first in php code

According to this manual: http://us2.php.net/setcookie I have to set the cookie before anything else.
Here is my cookie code:
if (isset($_COOKIE['watched_ads'])){
$expir = time()+1728000; //20 days
$ad_arr = unserialize($_COOKIE['watched_ads']);
$arr_elem = count($ad_arr);
if (in_array($ad_id, $ad_arr) == FALSE){
if ($arr_elem>10){
array_shift($ad_arr);
}
$ad_arr[]=$ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
}
else {
$expir = time()+1728000; //20 days
$ad_arr[] = $ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
As you can see I am using variables in setting the cookie.
The variables comes from a mysql_query and I have to do the query first.
But then, if I do, I will get an error message:
Cannot modify header information - headers already sent by ...
The error points to the line where I set the cookie above.
What should I do?
UPDATE:
I do this before the setCookie part:
$ad_id=$_GET['ad_id'];
$query2 = "SELECT * FROM classified WHERE classified.ad_id = '$ad_id'";
$results2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_array($results2);
$cat = $row2['category'];
$action=$row2['action'];
$sql_table='';
$num_rows = mysql_num_rows($query_results);
if ($num_rows != 0){
HERE COMES THE SETCOOKIE PART
As others have suggested, make sure, you are not outputting any html or whitespace before you set your cookie.
This will fail because you are printing html before you set your cookie.
<p>
<?php
// your cookie code - note <p> tag before <?php tag
// ...
?>
This will also fail, because you are printing whitespace before you set your cookie.
 
<?php
// your cookie code - note the extra linebreak before <?php tag
// ...
?>
Also
<?php
// your cookie code - note the extra space before <?php tag
// ...
?>
If you use an UTF encoding for your php script (and if you are not in one of the english speaking countries, chances are that you do), make sure your editor is set that it does not include byte order mark (BOM) at the begining of every file. See http://en.wikipedia.org/wiki/Byte_order_mark for more detail on BOM.
The restriction is not that you must not do anything before setting your cookies, merely that you must no output anything before setting your cookies.
For example, let's say we want to get some data from the database, output it to the user and set it to the cookie.
<?php
$data = getDbData();
echo $data['field'];
setcookie('field', $data['field'], time()+86400, '/');
This will fail because we've output the data before setting the cookie. We can fix it by moving the output to after we set the cookie.
<?php
$data = getDbData();
setcookie('field', $data['field'], time()+86400, '/');
echo $data['field'];
Make sure that you do not print anything prior to adding header-based information (as cookies are).
I can't see any problems with the code, unless mysql outputs an error, which could cause this.
This is a shot in the dark, but make sure you don't have any whitespace (or anything else for that matter) before the opening php tags. Also make sure you don't have any trailing whitespaces after the closing php tags in the files you include.
The error message you showed us says that the headers were sent on the setcookie() line. Thus, you may be setting headers or cookies later in the code, which is causing the error. (Or so I believe, since I can't recall the error word for word and you cut it off at the critical point)

PHP Header redirect not working [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
include('header.php');
$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];
$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();
header('Location:index.php');
exit;
The above code keeps giving me an issue with the redirect. The error is the following:
Warning: Cannot modify header information - headers already sent by (output
started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in
/Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.
I am totally flummoxed by this. Does anyone know what I should be doing to make it work?
EDIT
header.php code:
<?php
include('class.user.php');
include('class.Connection.php');
$date = date('Y-m-j');
?>
<html>
<head>
<link rel=StyleSheet href="css/style.css" type="text/css" media=screen>
<title>Test</title>
</head>
<body>
<div id="page">
Look carefully at your includes - perhaps you have a blank line after a closing ?> ?
This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.
Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.
(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).
Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:
Note: HTTP/1.1 requires an absolute
URI as argument to Location:
including the scheme, hostname and
absolute path, but some clients accept
relative URIs. You can usually use
$_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to
make an absolute URI from a relative
one yourself:
COMMON PROBLEMS:
1) There should be NO output (i.e. echo... or HTML parts) before the header(...); command.
2) After header(...); you must use exit();
3) Remove any white-space(or newline) before <?php and after ?> tags.
4) Check that php file (and also other .php files, that are included) -
they should have UTF8 without BOM encoding (and not just UTF-8). Because default UTF8 adds invisible character in the start of file (called "BOM"), so you should avoid that !!!!!!!!!!!
5) Use 301 or 302 reference:
header("location: http://example.com", true, 301 ); exit;
6) Turn on error reporting. And tell the error.
7) If none of above helps, use JAVASCRIPT redirection (however, discouraged method), may be the last chance in custom cases...:
echo "<script type='text/javascript'>window.top.location='http://website.com/';</script>"; exit;
Alternatively, not to think about a newline or space somewhere in the file, you can buffer the output. Basically, you call ob_start() at the very beginning of the file and ob_end_flush() at the end. You can find more details at php.net ob-start function description.
Edit:
If you use buffering, you can output HTML before and after header() function - buffering will then ignore the output and return only the redirection header.
Try This :
**ob_start();**
include('header.php');
$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];
$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();
header('Location:index.php');
**ob_end_flush();**
exit;
Look at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php line 15.
At that position, it makes some output. Fix it. :)
If I understand correctly, something has already sent out from header.php (maybe some HTML) so the headers have been set. You may need to recheck your header.php file for any part that may output HTML or spaces before your first
EDIT: I am now sure that it is caused from header.php since you have those HTML output. You can fix this by remove the "include('header.php');" line and copy the following code to your file instead.
include('class.user.php');
include('class.Connection.php');
$date = date('Y-m-j');
You may have some "plain text" somewhere in php files that is interpreted as script output. It may be even a newline before or after the php script tag specifier (less-than + question mark + "php").
Besides, if I remember correctly, according to http specification, the "Location" header accepts only full URLs, not relative locations. Have that in mind too.
Don't include header.php. You should not output HTML when you are going to redirect.
Make a new file, eg. "pre.php". Put this in it:
<?php
include('class.user.php');
include('class.Connection.php');
?>
Then in header.php, include that, in stead of including the two other files.
In form.php, include pre.php in stead of header.php.
Your include produces output, thereby making it impossible to send a http header later. Two option:
Move the output somewhere after the include.
Use output buffering, i.e. at the very start of your script, put ob_start(), and at the end, put ob_flush(). This enables PHP to first wait for all the output to be gathered, determine in what order to render it, and outputs it.
I would recommend you learn the second option, as it makes you far more flexible.
also try include_once() instead of include() that can also work
Also see your php file text encoding. Mine was UTF-8 with BOM and it prevented the script to work. But now works flawlessly after removing the BOM...
Try redirection with JavaScript:
<script type="text/javascript">
window.location.href='index.php';
</script>

Categories