Lets say that my domain is www.domain.com. I want to add GET variable to the every url which starts with domain.com. For example if user enters domain.com in browser, default get variable will be added so url will look like: domain.com?variable=default or if user enters domain.com/contact.php it will look like domain.com/contact.php?variable=default.
Thanks
You can include a say header.php file in all pages:
if ( !isset($_GET['variable']) ) {
header('Location: ' . $_SERVER['REQUEST_URI'] . '?variable=default');
}
Why not use it like this?
if (isset($_GET['var'])) {
$var = $_GET['var'];
} else {
$var = "default";
}
So if the var exist in URL, use that one. Else, use the default variable.
Or you could use a session to retrieve a variable that cannot be changed by the user.
// On first page
if (!isset($_SESSION)) { session_start(); }
$_SESSION['var'] = "var";
///////////////////////////////////////////////////////
// On second page
if (!isset($_SESSION)) { session_start(); }
if (isset($_SESSION['var'])) {
$var = $_SESSION['var'];
} else {
$var = "default";
}
Related
Okay, so this is what I do:
I go to www.mywebsite.com/orders?id=1
It redirects be to login before proceeding.
I log in successfully but it redirects to www.mywebsite.com/orders.
If I am already logged in and go directly using GET method, it works fine. But if I am asked to login, the GET method disappears.
How do I preserve ?id=1?
Before redirecting the user back to the login page store the current page (the requested page) in a session variable. Assuming you have a function called check_login this would more or less look like what you should do:
public function check_login() {
if (!$this->session->has_userdata('logged_in') || $this->session->logged_in !== true) {
if (!empty($_SERVER['QUERY_STRING'])) {
$uri = uri_string() . '?' . $_SERVER['QUERY_STRING'];
} else {
$uri = uri_string();
}
$this->session->set_userdata('redirect', $uri);
redirect('/auth/login');
}
}
Then when the user successfully logs in your login function should somewhere have the following logic:
public function login() {
// form validation
// get post vars
// check username/pwd against db
if ($login) {
if ($this->session->has_userdata('redirect')) {
redirect($this->session->redirect);
} else {
redirect('/dashboard');
}
} else {
// error logging in
}
}
session variable could store the id.While log in using session pass the id value.You can retrive the value anywhere in session.
$this->load->library('session');
$this->session->set_userdata('userId', 'YourId');
where userId would be the name of the session variable, and YourId would be the value.
Simply Use this
redirect($_SERVER['HTTP_REFERER']);
I have a simple login system with sessions. The user is being redirected to a page when the user succesfully logs in.
The problem is when the user leaves my site and comes back later to index.php (the same session) the user will get "Undefined index" because there's no parameter supplied when the user enter my site and is still logged in.
I use php switch to control my pages.
I have this code first in my index.php:
require_once('function.php');
session_start();
if (!is_user()) {
redirect('signin.php');
}
?>
My file with switch looks like this:
<?php
$p=$_REQUEST['p'];
if (isset($p)) {
switch ($p) {
case "vine":
include "vine.php";
break;
}
?>
Obviously $_REQUEST['p'] is undefined.
If you want your script to still know the p parameter when a user returns, you must somehow save it for further requests. This could be done like this in index.php:
<?php
session_start();
$p = isset($_REQUEST['p']) ?
$_REQUEST['p'] : (
isset($_SESSION['p']) ?
$_SESSION['p'] :
false
)
);
if ($p !== false) {
$_SESSION['p'] = $p;
switch ($p) {
case "vine": include "vine.php";
break;
}
} else {
die ('Unknown category ....');
}
?>
The code looks for an explicitely given parameter p and takes this if available. Otherwise it looks for a session parameter p.
Else it sets p to false to indicate that no value is avaible.
If a value for p is given, the session variable $_SESSION['p'] is set. And, of course, sesssion_start() must be called at the top of the script to make session variables available.
I assume the 'Invalid index' comes from $p=$_REQUEST['p'];. You want to check whether that array element exists.
if (isset($_REQUEST['p'])) {
What about this:
if (isset($_REQUEST['p']))
{
$p = $_REQUEST['p'];
// ...
}
But notice this: http://php.net/manual/en/function.array-key-exists.php#example-5520
I am using jquery throughout a php project, so all pages load dynamically into the main page, I am trying to make links only visible to a certain user, so if they goto the main page and append the URL, i.e ?mode=55rt67 this gets stored as a variable and can be checked throughout the app. I am using the below, but it doesnt work. any suggestions?
if (empty($_GET)) {
$mode = "user";
}else{
define ('$mode', '($_GET['mode']);
}
define is used to declare constants, you want to use a variable, not a constant.
UPDATED (use session to store mode variable):
if (empty($_GET)) {
$_SESSION['mode'] = "user";
}else{
$_SESSION['mode'] = $_GET['mode'];
}
And don't forget to use session_start on every page
Change your code with below code.
// Try this
if(isset($_GET['mode']) ){
define ('mode',$_GET['mode']);
}else{
define ('mode',"user");
}
echo mode;
//OR
if(isset($_GET['mode']) ){
global $mode = $_GET['mode'];
}else{
global $mode = "user";
}
echo $mode;
if (empty($_GET)) {
$_SESSION['mode'] = "user";
}else{
$_SESSION['mode'] = $_GET['mode'];
}
The above solution worked perfectly, added session_start to the index page, and it now carries through the rest of the pages.
Awesome, thanks all
I have problems with php multilanguage. I'm using function *check_lang* and it works fine in one page, but once I go to another page the $_SESSION['lang'] variable $lang turns back into default (en). What is the problem?
<?php
function check_lang() {
if(isset($_GET['lang'])
{
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang
}
if (!isset($_SESSION['lang'])) {
$lang = 'en';
} else {
$_SESSION['lang']=$lang;
}
//directory name
$dir = 'languages';
return "$dir/$lang.lng";
}
?>
You have to:
session_start();
At the top of each of your scripts in which you want to use session variables.
You need to call session_start() on each page you're planning on using the $_SESSION[] global in. That's what tells PHP that it should look up the session_id from the user's cookies or the query string so that PHP knows which session's values to use.
Reference.
Im Using the following code to validate (poorly, I know) a form's $_POST[]ed data. How can I pass $auth onto the new page so I can print it for the user? (short of using my-account.php?auth=You Must......)
if (!$_POST['supportarea'] || !$_POST['supportbody']) {
$auth = 'You must provide some information if we are ever to help you with your support ticket';
header('Location: ../pages/my-account.php');
}
Thanks. Alex.
You should use the session. In your script:
session_start();
$_SESSION['flash'] = 'You must ...';
In the my-account.php script:
session_start();
if (!empty($_SESSION['flash'])) {
$flash_message = htmlspecialchars($_SESSION['flash']);
unset($_SESSION['flash']);
} else {
$flash_message = null;
}
Then, if !empty($flash_message), show it to the user.