TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   passing variables through a url (http://www.talkphp.com/absolute-beginners/3350-passing-variables-through-url.html)

sarmenhb 09-11-2008 10:26 PM

passing variables through a url
 
hello,

i have a url like this: index.php?category=something

when i try to echo out $category on my local computer it doesnt work but when i try it on a hosting server it works

why wont it work locally?
i put register_globals to on.

anyone know?

ETbyrne 09-11-2008 10:29 PM

What's your code? Are you using short PHP tags?

sarmenhb 09-11-2008 10:33 PM

Quote:

Originally Posted by ETbyrne (Post 18329)
What's your code? Are you using short PHP tags?

no im doing long tag <?php

ok here is my code

this is the left navigation categories so when one of them is clicked the page will redirect to it.
Code:

<?php
#======================================
#  DISPLAY CATEGORIES
#======================================

$query = mysql_query("select category from tbl_category where visible = 'yes'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {


echo "<a href=\"index.php?category=d\">{$row['category']}</a><br>";
}

#======================================
?>

once redirected it will show this

Code:

<?php
#======================================
#  Listing
#======================================

echo "Category:".$category;

#======================================
?>


ETbyrne 09-12-2008 01:07 AM

Shouldn't you declare $catagory before using it?

PHP Code:

<?php
#======================================
#  Listing
#======================================

$catagory $_GET['catagory'];

echo 
"Category:".$category;

#======================================

# or you could do this...

echo "Category:".$_GET['category'];
?>


sketchMedia 09-12-2008 09:35 AM

Steer well clear of register_globals, they are downright daft as well as posing a considerable security issue if not done correctly.

Use the super globals:
PHP Code:

$_GET[];
$_POST[];
//or even use request, which is a fudge of the two above arrays as well as cookie data
$_REQUEST[]//contains  GET POST and COOKIE 


ReSpawN 09-12-2008 01:36 PM

Best way to pass it it definitly through GET's, unless you're posting (huge) amounts of data. Then, of course, you can use POST or rather use a database.

For later use, you can also save it encoded into a Cookie, and remove it when you're done. (first setcookie with the data, and then the same but with empty values)

sarmenhb 09-12-2008 10:15 PM

my page doesnt have a form on it , im grabbing the variables directly from the url

so if the url was index.php?category=cell phones

if i did the following on the server it would work but not on my pc

echo $category , the output would show cell phones

if im not using a form method such as post or get how would post or get even work?

ETbyrne 09-12-2008 10:46 PM

Data that is passed through a URL is considered form data.

Just try this:
PHP Code:

echo $_GET['category']; 

so if you view the page index.php?category=Yeah Toast the output would look like this:
Code:

Yeah Toast

ReSpawN 09-13-2008 11:20 PM

An even better way, to make sure that it all comes through, is base64 encoding.

There are so much ways to do this. I mostly do this trough encoding the string that I want to send, and then decoding it on the other send.

?this=a2lua3ksIGp1c3QgdHJ5aW5nIHRvIHNlZSBpZiB5b3UgYXJlIG dvaW5nIHRvIGRlY29kZSB0aGlzLg==

Base64 Encoder and Decoder

What you can do, if you want to send entire arrays, first serialise() the array, and then encode it as base64. When you want to retrieve it, decode it and then unserialise() it.

Salathe 09-13-2008 11:47 PM

Quote:

Originally Posted by sarmenhb (Post 18349)
if im not using a form method such as post or get how would post or get even work?

GET and POST are not simply 'form methods' but varieties of HTTP Request Methods, as such forms aren't necessary to send such requests. Indeed, a normal page request like the one that brought you to this page will have been using a GET request and any query string arguments (if any) will be available in the superglobal $_GET array.


All times are GMT. The time now is 05:50 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0