Append a GET variable on to a page with another GET variable - php

I have a profile.php page with a variable uid?=12 and i have a language bar on that is on a separate page called lang.inc.php:
Profile Page:
<div class="langbar">
<?php include 'inc/lang.inc.php'; ?>
</div>
lang.inc.php
<center>
<a class="flag_USA" title="English" href="<?php echo basename($_SERVER['PHP_SELF']); ?>"><img src="css/images/us.png"></a> <span> </span>
<a class="flag_France" title="French" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=fr"><img src="css/images/fr.png"></a> <span> </span>
<a class="flag_dutch" title="Dutch" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=de"><img src="css/images/de.png"></a> <span> </span>
<a class="flag_Italy" title="Italian" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=it"><img src="css/images/it.png"></a> <span> </span>
<a class="flag_Italy" title="" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=es"><img src="css/images/sp.png"></a> <span> </span>
<?php echo $_SERVER['PHP_SELF']; ?>
</center>
As you can see im trying to append the ?lang=fr for example to the end of the profile.php?uid=12 so it looks like this profile.php?uid=12&lang=fr but all it's doing is profile.php?lang=fr.

I'm pretty sure $_SERVER['PHP_SELF'] does not include get variables, just the file path, so you'd have to add the uid to the href yourself.

<?php
$basename = basename($_SERVER['PHP_SELF']); // Lets store the basename
$basename_with_uid = $basename . "?uid=" . $_GET['uid']; // and append the uid from the URL. Make sure to do some validation if $_GET['uid'] exists.
?>
<a class="flag_France" title="French" href="<?php echo $basename_with_uid; ?>&lang=fr"><img src="css/images/fr.png"></a>

REQUEST_URI is probably what you are after:
<a class="flag_France" title="French" href="<?php echo $_SERVER['REQUEST_URI']; ?>&lang=fr"><img src="css/images/fr.png"></a>
If your page was profile.php?uid=12, the output should be:
profile.php?uid=12&lang=fr
Ref: PHP $_SERVER variables
edit You may still need to add basename() around REQUEST_URI, depending on what you are doing

Related

How to pass variable between two web pages with PHP without using session

I need to know How to pass variable between two web pages with PHP without using session , have a DIV link in which inside there is id i want to pass to another page when a link is clicked but i fail to do it. please help me:
firstpage.php ( where there is a link) :
......
<?php
$identity = $_POST['book_id'];
while($ResultsRow=mysql_fetch_array($res)) {
?>
<div class="col-md-2 offset-md-2">
<div class='thumbnail'>
<a href="shopping.php?<?php echo $identity?>" target="blank">
<input type="text" name="book_id" value="<?php echo
$ResultsRow['id'] ?>">
</div>
</div>
</a> <!-- end of link -->
..........
Secondpage.php (where i want to retrieve book id):
....
<p> <?php $myvarC = $_POST['identity'];
echo $myvarC;
?> </p>
....
I think you should try something like below:
firstpage.php
<a href="shopping.php?identity=<?php echo $identity?>" target="blank">
secondpage.php
<p> <?php $myvarC = $_GET['identity'];
echo $myvarC;
?> </p>
You can include it in the urlquery like
shopping.php?id=1234
and then read it on the other side.
You can do this by $_GET;
First Page
<a href="shopping.php?id=<?php echo $identity?>" target="blank">
Second Page
<?php $myvarC = $_GET['id'];
echo $myvarC;
?>
Create a correct url with parameters
<a href="shopping.php?id=<?php echo $identity?>" target="_blank">
And get that value using
<?php
$myVarC = $_GET['id'];
echo $myVarC
?>
First Page
<a href="shopping.php?identity=<?php echo $identity?>" target="blank">
Second Page
<p> <?php $myvarC = $_REQUEST['identity'];
echo $myvarC;
?> </p>

Codeigniter URI routing is not working

I am using codeigniter framework in my web development and passing the value of image id using php code such below
<a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>">
<img src="" />
</a>
and getting the value at controller with this code $imageId = $this->input->get('id');
With the get method I used, it will display the url like this :
http://my-domain-name/account/gallery/view?id=1
But I want to display the url like this :
http://my-domain-name/account/gallery/1
So what I did is setting in the router with this code but it's not working.
$route['account/gallery/(:num)'] = 'default/Gallery_controller/view/$1';
My web still able to show the template of the web if I type http://my-domain-name/account/gallery/1 but display error in the field that I fetch from database because unable to get the imageId
use Regex instead of :num in route.php
$route['account/gallery/([0-9]+)'] = 'default/Gallery_controller/view/$1';
And in your template file:
<a href="<?php echo base_url('account/gallery/'.$imageDetail['imageId']); ?>">
<img src="" />
</a>
Then you can access directly in your gallary controller in view function $id.
eg:
class Gallery extends MY_Controller {
public function view($id){
// Your Image ID = $id;
}
}
Set this url http://my-domain-name/account/gallery/1 proper in href
<a href="<?php echo site_url('account/gallery/view'.$imageDetail['imageId']); ?>">
<img src="" />
</a>
Replace
<a href="<?php echo base_url('account/gallery/view'); ?>?id=<?php echo $imageDetail['imageId']; ?>">
with
<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId']; ?>">
Check your config.php
$config['enable_query_strings'] = true;
to
$config['enable_query_strings'] = false;
In this try this (:any)
$route['account/gallery/(:any)'] = 'default/gallery_controller/view/$1';
Or
$route['account/gallery/view?(:any)'] = 'default/gallery_controller/view/$1';
In the routes lower case
This code is make your url like http://domain-name/account/gallery/1
Try this:
<a href="<?php echo base_url();?>account/gallery/view/<?php echo $imageDetail['imageId'];?>">
<img src="" />
</a>

Passing URL does not output variable with echo

The link page:
<a href="displayProduct.php?productID=<?php'.$pID.'?>">
The displayProduct.php page:
<?php
echo $_GET["productID"];
?>
This is my tester, to see if clicking on the link will pass the data from page 1 to displayProduct page.
It successfully passes the variable in the ' URL ' bar but the echo does not display that variable.
Am I missing something?
You missed an echo and have some weird concatenation.
<a href="displayProduct.php?productID=<?php'.$pID.'?>">
should be:
<a href="displayProduct.php?productID=<?php echo $pID; ?>">
<a href="<?php echo 'displayProduct.php?productID=' . $pID; ?>">
The shortest way:
<a href="displayProduct.php?productID=<?php echo $pID ?>">

get file name for url php

I wanted to know how to get the current file name in PHP to be able to load the same file in a different directory.
index.php
works.php
lang/de/
index.php
works.php
lang/pl/
index.php
works.php
<div id="language">
EN
<a class="lang" href="index.php">DE</a>
<a class="lang" href="../pl/index.php">PL</a>
</div>
The current method is redirecting the lang URL, allways to index.php.
I would love to know how to redirect from works.php to lang/de/works.php without redirecting to lang/de/index.php
Sorry if i'm getting you confused
I think this is what you want:
<?php
$page = basename($_SERVER['PHP_SELF']);
?>
<div id="language">
EN
<a class="lang" href="<?php echo $page;?>">DE</a>
<a class="lang" href="../pl/<?php echo $page;?>">PL</a>
</div>
You would save the current file name in the $page variable. Then echo it out and replace index.php in your links.
You should really google this stuff.
<?php basename($_SERVER['PHP_SELF']); ?>
GOOGLE
Now that you added some code, here ya go.
<div id="language">
EN
<a class="lang" href="<?php echo basename($_SERVER['PHP_SELF']); ?>">DE</a>
<a class="lang" href="../pl/<?php echo basename($_SERVER['PHP_SELF']); ?>">PL</a>
</div>
FYI, basename($_SERVER['PHP_SELF']); get's the name of the current file and returns just that. In other words, it will return "index.php" or "works.php"

mysql fetch assoc used in url fetching

i have a script in which output localhost/urlfromMysqlDatabase
i need output should be urlfromMysqlDatabase
please give me any suggestions to changes this script
$link = mysql_fetch_assoc(mysql_query("SELECT url FROM admins"));
and in between html body i have a code
<a href="<?php echo $link['url']; ?>" target="_self">
Try this:
<a href="<?php echo #end(explode("/", $link['url'])); ?>" target="_self">
end(explode("/", $link['url'])) will print out urlfromMysqlDatabase (everything after the last slash /).
$link = mysql_fetch_assoc(mysql_query("SELECT url FROM admins"));
$link=end(explode("/",$link['url']));
<a href="<?php echo $link; ?>" target="_self">

Categories