how to read data from variables using header function in php - php

in the root of my project folder i have a file called index.php and a directory called views which contains a file called page_one.php ...
index.php file has the follwing code:
<?php
$action = "one";
if ($action == 'one') {
$name = "John";
//include './views/page_one.php';
header("Location: views/page_one.php");
}
?>
and page_one.php has the following code:
<?php echo 'Name = ' . $name; ?>
in the above code I have commented out the line with include because that works perfectly..I want to pass the value of $name using the header function..Is there a way of doing it WITHOUT sending the value in the URL?
I want the address in the URL to change when page_one.php is accessed that is why I am using the header function instead of include...

#Qirel as per said, try using session. Do something like this in index.php
<?php
session_start();
$_SESSION['name'] = "John";
header("Location: views/page_one.php);
exit();
?>
and inside page_one.php
<?php
session_start();
echo $_SESSION['name'];
unset($_SESSION['name']); // remove it now we have used it
?>

Related

Can't pass variable from one file to another using $GLOBALS array [duplicate]

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

How to call a script every time a user logs in (or whenever a session is active)

I am trying run a script called random_post_generator.php which should execute every time a user is logged in.
I am using this approach as an alternative to cron.
Here is how my session is currently created:
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
}
?>
But how do I say - "if session is active, then run this script"?
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
include 'random_post_generator.php';
}
?>
or you can use require 'random_post_generator.php'
If I understood correctly, you are trying to find out how to include a script of php (that is located in an outside .php file) inside your current file while using your previous code that checks if a user is logged in:
<?php
$root_directory_path = $_SERVER['DOCUMENT_ROOT'];
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
$pathName = $root_directory_path."myScript.php";//I am assuming here
//the script is located inside the root directory, and not in a sub
//directory
require($pathName);
}
?>
just remember that whatever php code is inside myScript.php has to have the <?php ?> tag surrounding it. Your code does not reuse the <?php ?> tag of the "calling" file.
Let me know if that worked for you

How to include php file instead of url in php redirect

Instead of such regular redirect:
<?php
header('Location: http://domain.com');
?>
...I need to call the URL in from another php file instead of http://domain.com, so something like that:
<?php
header('Location: <?php include("file.php");?>');
?>
File.php has the right url in it (generated with API code). But it doesn't read the php code in php redirect. How to make it work?
Try this:
file.php
<?php
// Determine your URL and assign it to $url
$url = "http://example.com";
?>
File with header redirect
<?php
require_once('file.php');
header('Location: '.$url);
?>
It depends how your php file pass the generated url.
If it use the echo use next:
ob_start();
include('file.php');
$output = ob_get_clean();
header('Location: '.$output);
Or if it use the return:
$return = include('file.php');
header('Location: '.$return);

How to access a variable across two files

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

Create a function that sets $_SESSION variables

I created a site where you need to login to visit the different pages, nothing special.
To keep the user logged in, I'm setting the session on top of every page.
My problem is, I don't wanna have to set the different session variables on top on each page. I'd rather have one function I can call to set them. Plus I don't need all those variables on each page, so I'd like the function to accept optional parameters (like the email, or profile picture that are not used on every page).
I call this on top of each page:
<?php
require_once 'session.php';
confirm_logged_in();
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$picture = $_SESSION['picture'];
$group = $_SESSION['group'];
?>
I would like to make it more like this and be able to set only the variables I need:
<?php
require_once 'session.php';
confirm_logged_in();
set_variables($username, $email);
?>
The 'session.php' file is like this:
<?php
session_start();
function logged_in(){
return isset($_SESSION['username']);
}
function confirm_logged_in(){
if(!logged_in()){
header('location: start.php');
}
}
?>
I've tried a few things, but it just led me to huge amounts of errors.
Has someone already done this or found a script doing this? Is that possible?
First of all, if what you want to do is overload your function, you can't do that. For more info on that see this. However, you can do this:
<?php
set_variables($username, $email, $picture,$group)
{
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['picture'] = $picture;
$_SESSION['group'] = $group;
}
?>
Put this function in your session.php file.
I am not sure if I understood right, but if I did, all you need to do is create a new file, let's call it "Session_Variables.php".
After you created the file, paste the following code into it:
<?php
require_once 'session.php';
confirm_logged_in();
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$picture = $_SESSION['picture'];
$group = $_SESSION['group'];
?>
Then, finally, just replace the old code with:
include("Session_Variables.php");
Not directly related to the question you are asking, but you should really add exit; after a redirect header. Clients can ignore headers and still load your page even while not being logged in.
if you want to make set_variables($username, $email) work like i think you wanted, you need to write something like this.
Session.php
<?php
session_start();
function logged_in(){
return isset($_SESSION['username']);
}
function confirm_logged_in(){
if(!logged_in()){
header('location: start.php');
}
}
//Only Picture and group are Optionals
function set_variables($username, $email, $picture = '', $group = ''){
//you can check here is thoses variables are set or valid before assign them
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['picture'] = $picture;
$_SESSION['group'] = $group;
}
//create a function that we need to retrieve thoses values
function get_variable($name){
if ( isset( $_SESSION[$name] ) ) return $_SESSION[$name];
return FALSE; //if the variable is not set.
}
?>
And you can use it like this
<?php
require_once 'session.php';
confirm_logged_in();
set_variables($username, $email);
$username = get_variable('username');
?>
I think you need to move the session_start(); to the actual page. Using a require_once on the session_start(); is not a good plan.

Categories