send email (with GET params) from another php file - php

I have defined an email function in file: wel1.php
I need to call it in the middle of another php file, I'm trying to call it with:
include 'bienvenido/wel1.php?to="'.user.'"&pass="'.pass.'"';
I can correctly send email if I just type this in my browser's address bar:
bienvenido/wel1.php?to=some_email#gmail.com&pass=password
What am I missing?
Regards.

I think you can't pass GET parameters like this.
Thanks to the same variable scope used for included files, you can simply set $_GET variable before inclusion. Like this:
$_GET['to'] = $user;
$_GET['pass'] = $pass;
include 'bienvenido/wel1.php";
You will get what you want

Dollar signs for your variables:
include 'bienvenido/wel1.php?to="'.$user.'"&pass="'.$pass.'"';

you forgot the $ in front of the variable name: $user and $pass
So try: include 'bienvenido/wel1.php?to="'.$user.'"&pass="'.$pass.'"';

Related

Using variables in header script

I want to pass some information to another page using "header" but I also want to use a variable for the location, for example:
With a static URL in location:
header("Location: mypage.com?fname=$_POST['fname']&lname=$_POST['lname']");
I want to use a variable like so:
header("Location: $myVariable?fname=$_POST['fname']&lname=$_POST['lname']");
But I can't for the life of me get the syntax right, as shown above it will throw an error, any help is appreciated.
Tom
You need to set a full url (with http://) and use put arrays between parentheses {}.
Working well with a static url:
header("Location: http://mypage.com?fname={$_POST['fname']}&lname={$_POST['lname']}");
Working well with a url variable:
$url = 'http://mypage.com';
header("Location: $url?fname={$_POST['fname']}&lname={$_POST['lname']}");
I hope this helps!
There are two way you can achive this, one is use session and another this url strucutre,
In session :
session_start(); // this should be on top of login_check file
// this goes just before redirect line
$_SESSION['fname'] = $_POST['fname'];
Or use url structure as follows,
header(wwww.example.com?action='')

How to access a variable from one PHP page to another, without a form, link, or button?

TLDR:- What is a good way to pass contents of a variable from one PHP file to another without involving a form, link or a button.
Question:-
So there is a form in a page/file called question_edit_form.php and its action attribute is already set to another file called question.php. The variable of interest is being read-in from the user in question_edit_form.php and is then obviously being sent to question.php using $_POST.
Now, there is a third file, named renderer.php, and which is not linked to the other two files. I want to use that variable of interest in this file. So how can I access that variable which is set in question.php from inside renderer.php?
first file -
session_start();
$_SESSION['your_variable'] = 'value';
other file -
session_start();
$var = $_SESSION['your_variable'];
this may help.
It sounds like you are using Moodle, in which case renderer.php is not an independent file; it contains the class definition for the renderer object used by question.php.
So... there is no need to pass the parameter between the scripts. If you really must access the form value directly from the renderer, just use the standard methods from the Moodle framework: required_param($name, $type) or optional_param($name, $default, $type).
Generally there are two methods available for you to pass on the value
Cookies
Sessions
How to use cookies:-
setcookie(name, value, expire);
e.g.
setcookie("user", "Alex Porter", time()+3600);
Access it using echo $_COOKIE['user'];
Second is Sessions. Here is how to use sessions:-
session_start();
$_SESSION['varname']=value;
Accessing page:-
session_start();
echo $_SESSION['varname'];
Additional info if required:-
Make sure you use session_start() at top of your page if not you may face with an headers already sent error / warning which again can be fixed by output buffering ob_start()
You can store the variables in the session.
http://www.w3schools.com/php/php_sessions.asp

How to pass variables from one php page to another without form?

I want to know how to pass a variable from one page to another in PHP without any form.
What I want to achieve is this:
The user clicks on a link
A variable is passed which contains a string.
The variable can be accessed on the other page so that I can run mysql queries using that variable.
use the get method in the url. If you want to pass over a variable called 'phone' as 0001112222:
<a href='whatever.php?phone=0001112222'>click</a>
then on the next page (whatever.php) you can access this var via:
$_GET['phone']
You want sessions if you have data you want to have the data held for longer than one page.
$_GET for just one page.
<a href='page.php?var=data'>Data link</a>
on page.php
<?php
echo $_GET['var'];
?>
will output: data
You can pass via GET. So if you want to pass the value foobar from PageA.php to PageB.php, call it as PageB.php?value=foobar.
In PageB.php, you can access it this way:
$value = $_GET['value'];
check to make sure the variable is set. Then clean it before using it:
isset($_GET['var'])?$var=mysql_escape_string($_GET['var']):$var='SomeDefaualtValue';
Otherwise, assign it a default value ($var='' is fine) to avoid the error you mentioned.
You can use Ajax calls or $_GET["String"]; Method
If you are trying to access the variable from another PHP file directly, you can include that file with include() or include_once(), giving you access to that variable. Note that this will include the entire first file in the second file.

PHP passing a variable with header redirect

I am trying to pass a variable within a header link. I have tried below code
$name = "mike";
if($name != "lopan"){
header("Location:error-page.php?$errorMssg=Please enter information?");
}
This page redirect to location but doesn't pass the variable that contains the message. But when i create a simple link with values like this:
link
it passes it just fine.
Any ideas what i am doing wrong? or i can not pass information with headers?
You need to use urlencode like this:
if($name != "lopan"){
header("Location:error-page.php?errorMssg=".urlencode("Waoo so your not EVEN going to try to enter information huh?"));
}
And in error-page.php, you should get it (no need to urldecode):
<?php
$errorMssg = $_GET['errorMssg'];
Remove the $ before errorMssg and urlencode the message.
could it be because you have $errorMssg instead of $errorMsg ? also try to make a valid url, for example replace " " with %20 etc, function urlencode() could help you with this.

In PHP :how can I use include() Function which parameter is variable?

I need to use include Function with variable.
but,when I try to do it I faced some errors .
Code :
$year=$_POST['year'];
$month=$_POST['month'];
$day=$_POST['day'];
include "Event.php?year=".$year."&month=".$month."&day=".$day;
so,can U help me ? : )
When you include files, you can't pass them any arguments. However they DO inherit any variables in the current scope (global or method).
I'm guessing at what your code does, but I assume you can just do this:
include "Event.php";
You dont need to use that kind of sintax.
The $year, $month and $day variables will be perfectly visible and accessible on the Event.php file.
To get along with what you want, check PHP manual on http://php.net/manual/en/function.include.php to see some examples.
I'm not sure what you're trying to achieve, but I have a couple of guesses handy.
1 - You are trying to redirect the user, with those parameters appended to the url, in which case you will probably need to utilise header:
header("Location: http://example.com/Event.php?year=$year&month=$month&day=$day");
2 - You are trying to capture the output of that page given the presence of certain GET parameters, in which case you can utilise file_get_contents:
$output = file_get_contents("http://localhost/Event.php?year=$year&month=$month&day=$day");
Include the Event.php directly and access the $_POST or $_GET variables from there.
include('Event.php');
// Event.php:
echo $_POST['year']; // works

Categories