I am making a report problem link on my website and I want it to email me the last address they were on before clicking on the report link. I know/think you use $_SERVER['HTTP_REFERER'] but I dont know how to put that in the mail code?So how would you write that here is my mail code with out it.
Mail("email#email.com", "Subject", "Message");
echo "Report Sent";
The message should be a variable that you can put information in:
$message = "Error report: <p>Last site visited: {$_SERVER['HTTP_REFERER']}</p>....";
mail("email#email.com", "Subject", $message);
Note that the HTTP_REFERER bit is placed within {} in the string. That's to force it to extrapolate the value (I don't like string concatenation).
Also note that, as has been said above, there's no guarantee that the REFERER will have the right value, or any value at all.
Beside everything that has been told about http referers that can be sniffed, anonymizing proxies and so on, relying on the HTTP_REFERER is not a good programming standard.
Instead, if you have, for example:
http://www.example.com/application/client.php
Where users can click on
http://www.example.com/application/report_problem.php
Just pass the "...client.php" string to the "...report_problem.php" report problem handler you will create.
It's easy to pass the "originating" page link to the "report_problem", and can be done like this:
<?php
// pages where you will append the "problem link"
// $this_page holds an "url encoded" version of the request uri ( e.g. /application/client.php )
$this_page = rawurlencode ( $_SERVER["REQUEST_URI"] );
?>
Report problem
Then, in the "report_problem.php" code:
<?php
// report_problem.php
$originating_page = ( array_key_exists ( 'originating_page', $_GET ) && ! empty ( $_GET['originating_page'] ) ? rawurldecode ( $_GET['originating_page'] ) : null;
if ( ! empty ( $originating_page ) ) {
$message = 'Error report: <p>Last site visited: ' . $originating_page . '</p>....';
mail("email#email.com", "Subject", $message);
}
else mail("email#email.com", "Subject", "Problem from unkown page");
?>
Related
Im making an email verification system on PHP in cPanel, and when i press register, it sends an email to me with a link like this "../verified.php?code=1cbb402a59e8ec26dac0", i would need to get the full link and would have to chop it so it leaves me with just the code "1cbb402a59e8ec26dac0" so i can check if the code exists in database and then verify the account.
So from this
../verified.php?code=1cbb402a59e8ec26dac0
To This
"1cbb402a59e8ec26dac0
Purchasing the hostings for the first time fried my brains, so would be thankful if anyone could help me,
For getting the text after the code: in the link, you can use the PHP $_GET function. You can use this code in your verified.php to get the text after code:
<?php
if(isset($_GET['code']))
{
$Data=$_GET['code'];
}
else{
echo "Invalid Email Verification";
?>
Now the part after the code= gets stored in the variable Data.
You can change that to your desired variable.
Sometimes even when the code is set, it might be empty, so to check that, the empty() function in PHP can be used.
You can add this code to your verified.php:
<?php
if (empty($Data))
{
echo "Invalid Verification ID";
}
else
{
echo "Email Verification Success";
//your other codes to update it on the server
}
?>
Well you have chosen to pass parameters which will be available in the global $_GET[] (as opposed to $_POST[]).
So in your verified.php you will need to examine for the presence of $_GET['code'] and take the appropriate action.
Using $code = $_GET['code']; is very bad as you need to qualify it.
At a minimum you would need to...
<?php
// Ternary to give $code a value to prevent undefined variable error.
$code = isset($_GET['code']) ? $_GET['code'] : NULL;
if($code === NULL){
// error
} else {
// Check its a valid code and take the appropriate action.
}
I am writing an inventory Management system. I am mostly complete with it. It is pretty basic, but does the job.
So now I am working on the look of it. I placed a Searchbox/Query inside a div at the top of the page. The Search works. But it only displays the result on the page that is listed.
What I want to do is have the Search redirect to this page when the button is Submitted, but It is not working. So I am wondering where to put the header("Location: loggedin.php");
I have gotten this to work in different parts of the Site, but for some reason it is not working here.
<?php
session_start();
if( isset( $_POST['Search'] ) ) {
// build a function to validate data
function validateFormData( $formData ) {
$formData = trim( stripslashes( htmlspecialchars( $formData ) ) );
return $formData;
}
$formEmail = validateFormData( $_POST['email'] );
include('connection.php');
$query = "SELECT first_name, last_name, email, card_number, pc_type
FROM
profiles WHERE email ='$formEmail'";
$result = mysqli_query( $conn, $query );
if( $formEmail == $email ) {
session_start();
$_SESSION['email'] = $email;
header("Location: profilepage2.php");
}
}
mysqli_close($conn);
?>
I do not think the HTML should matter in this case, but if I am wrong I will post it in an edit.
You can put your header("Location: your-url") anywhere in your script but it should be placed before you send any output as defined in the manual here http://php.net/manual/en/function.header.php.
You need to add exit; immediately after the header which is very important, Otherwise the script execution will not be terminated.
you must use like,
header(“Location: loggedin.php”);
exit;
If redirect is not working then try to add ob_start at the first line of your script just next to the open PHP tag like below,
<?php ob_start();
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
is it possible to do this , im trying to validate a form then, it will redirect using header() if TRUE.. but it seems not to be working? or my method is completely wrong ?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["clientEmail"];
if ($email != $sentEmailClients) {
echo 'Please enter a valid email';
} else {
$newURL = "http://www.myurl.com";
header('Location: ' . $newURL);
}
}
Give us more details about what actually happens when you run your code. You're likely facing one of the following problems:
You're using header() after you've already sent output to the browser. Headers must be sent before any other output. Check out the docs. If you change that line with die('redirecting') and that text shows up, then this is your problem.
Request method is not POST. Add die($_SERVER['REQUEST_METHOD']). If something other than POST is printed, then this is your problem.
$_POST['clientEmail'] is not set, or is not equal to $email
$email is not what you expect (where does it come from?)
$sentEmailClients is not what you expect (where does it come from?)
Basically, "why doesn't it work?" is not a good question because it doesn't give us much info with which to help you. Be more specific about what is happening.
Show enough of your code that we understand the origin of the variables you use.
Hi It seems that Your outermost if condition is not working thats why your header function is not working i just tried this and it woks fine. That means either your first if condition is false or either second if condition becomes true every time just try to echo your values before checking them.
<?php
if (1) {
$email = $_POST["clientEmail"];
if (0) {
echo 'Please enter a valid email';
} else {
$newURL = "http://www.google.com";
header('Location: ' . $newURL);
}
}
check this print_r($_SERVER["REQUEST_METHOD"]); is POST or not and
$email != $sentEmailClients true or false
I am still learning PHP and am now completely stuck - any help would be so much appreciated! Scenario: HTML form for user to complete, among other things, they have to select with a radio button how many tickets they want to buy. My PHP file compiles all the values into an email and sends it off - that part works perfectly - and then redirects the browser to my "thank you for completing the form" page. I would now like to display a value collected in the form on this thank you page: the amount of tickets the user selected with the radio button. How do i call the 'tickets' value to the thank you page?
Thank you ever so much!
Here is the HTML form:
http://menusolutions.co.za/maidens2014_booking_form.html
Here is my "sendmail3.php" file that sends my mail:
$webmaster_email = "carin#menusolutions.co.za";
$feedback_page = "maidens2014.html";
$error_page = "maidens_error_message.html";
$thankyou_page = "maidens_thank_you.php";
$name = $_POST['name'] ;
$telephone = $_POST['telephone'] ;
$cell = $_POST['cell'] ;
$email_address = $_POST['email_address'] ;
$address = $_POST['address'] ;
$tickets = $_POST['tickets'] ;
$mail_body = "Name: $name \n Telephone: $telephone \n Cell: $cell \n Email: email_address \n Address: $address \n Tickets: $tickets";
mail( "$webmaster_email", "Maidens Bowled Over 2014",
$mail_body, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>
And here is my thank you page, upon which I need to display the 'tickets' value so that people can be reminded how many tickets they bought and the amount they need to pay:
http://menusolutions.co.za/maidens_thank_you.php
Your help will be greatly appreciated! Thank you!
You can send ticket value inside URL or using session.
header( "Location:". $thankyou_page.'?ticket='.$tickets );
and on thanks you page add below code,
$tickets = $_GET['tickets'];
echo $tickets . ' tickets.';
The easiest way would be to redirect to the "thank you"-page and add the ticket value as a get-parameter
Link
maidens_thank_you.php?tickets=$tickets
You could use a GET parameter for this. For example,
header('Location: ' . $thankyou_page . '?tickets=' . $tickets);
And then in your thank you page (which I assume is a PHP script), you could retrieve that value using.
$tickets = $_GET['tickets'];
echo 'Thank you! You bought ' . $tickets . ' tickets.';
You can set a session or pass the values in the url by making your $thankyou page something like
$thankyoupage + "?numberoftickets=" + $numberoftickets;
header('Location:' + $thankyoupage);
and get the value back by
$_GET['numberoftickets']
in your actual thankyoupage.
only use the session method if you need the var in other pages than the thankyoupage too. sessions make this value available in your hole application.
more information about sessions can be found here: http://www.w3schools.com/php/php_sessions.asp
You can use sessions to accomplish that.
An other option is redirecting and using a GET value.
On your sendmail.php page you can use header to redirect the user :
header('location: thanks_page.php?ticketValue='.$ticketValue);
Be careful that : header() must be called before any actual output is sent. (see doc.)
And then get it back in your thanks_page in $_GET['ticketValue'] and do not forget to espace the value with htmlspecialchars or an equivalent for security !
Never use a varible in double quotes ("") it's will be string so use direct. you need to more learn php
for mail tuts :- http://php.net/manual/en/function.mail.php
mail( $webmaster_email, "Maidens Bowled Over 2014",
$mail_body, $email_address );
header( "Location:". $thankyou_page );
I'm trying to pass an error message from a server side form validator in a function back to the form it was submitted in. The validator is working as it prevents the rest of the code saving it to a database as planned. However I cant get it to pass back to the form to display the error
function saveComment(){
$validate = array();
$id = isset($_POST["articleId"]) ? $_POST["articleId"] : '';
if ( isset( $_POST['saveChanges'] ) ) {
if ( $_POST['name'] == "" ){
$validate['errorMessage'] = "Please fill out your name.";
header( "Location:".HOME_PATH."/.?action=viewArticle&articleId=".$_POST['articleID']."");
}
I' trying to pass it back to this
if ( isset( $validate['errorMessage'] ) ) {
echo $validate['errorMessage'];
}
When I remove the if on the display function I get the error unidentified index
What do I need to do to get the form to display the error message. Do I need to pass the array to the function that handles the display of the article?
FEEDBACK
For anyone that may find this useful I used #OliverBS post method pretty much unaltered.
Also thank you to #lethal-guitar as he explanation has helped me understand where I went wrong and the various methods that can be used to solve this problem +1
You're setting a variable $validate for your currently executing script. Afterwards, you send a redirect header. This will cause your browser to issue a new request, thus ending the currently executing script and scrapping the variable. The new request will trigger another script invocation, where the variable is not known anymore since it only existed for the duration of the first request.
HTTP is stateless, so every variable you set on the server side will only exist until you finish your current request and respond to the client. What you need is a way to pass this variable to the script handling the second request. There are several ways to do so:
Pass a GET parameter. You could append something like "&validationError=" . $validate['errorMessage'] to the URL you're passing to the Location header, and then in the display page access it via $_GET.
Save the validation status in the $_SESSION. The PHP manual contains a lot of information about sessions (maybe you're already using them?)
Restructure your code in a way that you don't redirect on error, but on success.
Some more information on the 3rd proposal: You write one PHP-Script which displays the form and handles the form post request. If validation fails, you simply redisplay, and insert the echo statement you already have. If it suceeds, you redirect to some success page. This way, the variable will remain accessible, since it's still the same request.
On a quick glance try this
Session way
Make sure to start the session by doing session_start(); at the top of the file where saveComment is and the isset checked.
function saveComment(){
$id = isset($_POST["articleId"]) ? $_POST["articleId"] : '';
if ( isset( $_POST['saveChanges'] ) ) {
if ( $_POST['name'] == "" ){
$_SESSION['errorMessage'] = "Please fill out your name.";
header( "Location:".HOME_PATH."/.?action=viewArticle&articleId=".$_POST['articleID']."");
}
if ( isset( $_SESSION['errorMessage'] ) ) {
echo $_SESSION['errorMessage'];
}
or you can try
POST way
function saveComment(){
$id = isset($_POST["articleId"]) ? $_POST["articleId"] : '';
if ( isset( $_POST['saveChanges'] ) ) {
if ( $_POST['name'] == "" ){
$error = urlencode('Please fill out your name');
header( "Location:".HOME_PATH."/.?action=viewArticle&articleId=".$_POST['articleID']. "&error=" . $error);
}
if ( isset( $_GET['error'] ) ) {
echo urldecode($_GET['error']);
}
I have not tested this but you should get the basic idea of what to do.
When you do a header location your redirecting the user to a new page. Your going to have to either pass the error in the query string or ideally pass it as a variable in the session.
I would suggest doing this all in one file, i.e. The form and the validation as one file.
Then you can do this:
<?php
//set success to 0
$success = 0;
$errormsgs = array();
//check for post
if(isset($_POST['submit'])){
//get the data from the form post and validate it
$valid = validateFuntion($_POST['data'])
//the is function will validate the data. If it is not valid, it will add a message to $errormsgs
//check for errors
if(!$errormsgs){
//data validation was successful, do stuff
}
}//if validation fails, it will fall out of the this code block and move on
?>
<html>
<body>
<?php
//check for errors
if($errormsgs){
$content .= '<ul class="errors">';
foreach($errormsgs as $error){
$content .= "<li>" . $error . "</li>";
}
$content .= "</ul>";
echo $content;
}
?>
<form name="name" action="" method="post">
<input name="name" value="<?= (isset($_POST['data']) ? $_POST['data'] : '') ?>" type="text">
</form>
</body>
</html>
You're redirecting the user to the "error" page with the header statement. The problem is, of course, this is a completely new page, there's no state left over, so none of your variables exist any more.
There's two ways to do it, either pass it on the query string (so add &error=...) and parse that in your template, or save it to the session.
Of course, you should really be doing this before your template is presented using a different means, but that's a complete rework of your code.