0

When passing a variable to another page via PHP I found that trying to find a solution to be above and beyond a pain as it is all over the place to find the relevant information regarding it such as how to send both single and multiple variables and how to select specific variables inside of a url and the security risks involved with url injections.

So far this is what I've managed to piece together.

Simple question:

Passing Variables from one page to another via URL-Bar

// PHP (Server side)
// To send it
    header(Location: www.example.com?VariableId=$Var)

// To get it
    $_GET["VariableId"]

// Sending multiple variables
    header(Location: www.example.com?VariableId1=$Var1&VariableId2=$Var2)

// To get a variable in a multiple variable url it's the same method
// just specify a different variable
    $_GET["VariableId1"]
    $_GET["VariableId2"]

However I have had difficulty understanding how to do something similar to the PHP in javascript (Client side), how to get a specific variable from the URL to use on the page.

To redirect in javascript like in PHP see link as it's the best one I've found. How to redirect to another webpage in JavaScript/jQuery?

I assume you can put ?VariableId=Blarg at the end of the links referenced in the link to make it work the same.

For security reasons I understand that it's unwise to use the url to send across variables as they can be used in ways unintended. I've done research on how do urlencode however came to no understanding and an explanation of how to use it (both encoding it and decoding it would be greatly appreciated) and is urlencode protection enough?

Community
  • 1
  • 1
Hamedar
  • 149
  • 1
  • 2
  • 11
  • First google result to read GET variables in JS: http://papermashup.com/read-url-get-variables-withjavascript/ -> Read URL GET variables with JavaScript – Milanzor Dec 30 '13 at 14:14
  • I guess you're looking for [sessions](http://www.php.net/manual/en/features.sessions.php). – georg Dec 30 '13 at 14:23
  • @Milanzor, this is my point. The writer of the website is has written the code, but doesn't comment / explain the code. I follow what he is doing, but have no idea how or what each line is doing. Learning why is what most of it is about. Just C-V'ing isn't what I am after and the level of explaining the author goes to is non-existent. That site has exactly what I want, but doesn't assist me in understanding it. – Hamedar Jan 02 '14 at 05:40
  • @thg435, sessions are an alternative method to what I want yes. So I will look into it but it's not what I am after here. – Hamedar Jan 02 '14 at 05:41

4 Answers4

0

i think you have to study the POST and GET methods:

use the following tutorial: http://www.tutorialspoint.com/php/php_get_post.htm

Yazan Malkawi
  • 501
  • 4
  • 10
  • First, thank you for the link, I'll wander around it for hours learning things. Second, just to confirm that I understand this: If the URL bar is http://www.example.com/index.php?Blarg=Test&Blarg2=Test2 if( $_GET["Blarg"] || $_GET["Blarg2"] ) This refers to the url via the $_Get command and selects a variable equal to the passed value? – Hamedar Jan 02 '14 at 05:51
  • $_get is not a command, it's an array holding the values passed in the url. and the if statement you typed above checks if the values are not null, usually you execute the code that uses these values – Yazan Malkawi Jan 02 '14 at 06:13
0

You may want to store variables in a cookie, you can do that easily in javascript with this plugin (for jQuery): http://plugins.jquery.com/cookie/

//To set a cookie
$.cookie('the_cookie', 'the_value');

//Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });

//Create expiring cookie, valid across entire page:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

See examples here: http://www.jquerybyexample.net/2012/06/jquery-cookies-get-set-and-delete.html

  • Cookies can be used, however not what I'm after in this context but I will use them soon, and I prefer to steer clear of plugin's that are not necessary. However thank you for this code, I will use it in the near future. – Hamedar Jan 02 '14 at 05:45
0

If you want to redirect page with javascript just used simple code as:

     <script>

      function redirecturl(){
       var url = "www.domain.com/yoururlstring"; 
       window.location = url; 
       }

     </script>
Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Rahul Bajaj
  • 375
  • 3
  • 14
  • Thanks for the response, but I wanted to pull a variable from the URL not redirect via Javascript. – Hamedar Jan 02 '14 at 06:01
0

If you want to store data on the server without exposing it to the client (because they could tamper with it), then you can store it in a user's session on the server. Think about a session as a pool of data that stays on the server between requests from the user.

So a user makes request 1. Then makes request 2. If you set something into the session on request 1, then you can fetch it from the session on request 2. The server automatically manages the storage and retrival of the

http://www.php.net/manual/en/book.session.php

<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?>
Jazzepi
  • 5,259
  • 11
  • 55
  • 81
  • This is a great idea, and I can see it's uses and applications however it wont with the method I am trying. – Hamedar Jan 02 '14 at 05:43