View Full Version : alter long url

07-24-2010, 02:10 AM
when using php the url at times can get very long with all the variables visible. is there a way to clean it up using javascript or some other method? if so can someone point the direction where and how to search for this?

07-24-2010, 02:26 AM
You cannot use a different URL if you want to load the same page. That information is required, or the server will not know it.
Additionally, you cannot use a fake URL in the address bar via Javascript, etc.

So that means you will need to approach this a new way.

There are a few options:

1. Use shorter variables and better names.

2. Find another way to store some of this: cookies, sessions, post variables... it depends on what your needs are, though having the information in the URL is usually the best since this allows for links and bookmarks, and it's the easiest to use.

3. Use .htacess and mod_rewrite to switch the ugly URLs from pretty ones. This is a popular technique, but it can be VERY difficult. It requires the use of "regex" (regular expressions) which can be, at first, very daunting. You can find some examples by searching, but you may need to adapt them to your specific setup. Basically what mod_rewrite does is takes one request URL then tells the server to give the user a different page, and the user never knows the difference. As a simple example, you could do 2.php instead of 1.php, but as a more useful one you could do ?var=value to /value.
I have created a tutorial/introduction to doing this while keeping most of the control in PHP. You may find this easier or harder than regular mod_rewrite, but I think it's easier (since I know PHP well, but not regex or .htaccess). It may help you.

Obviously (3) will give you the most control, but (1) and (2) may help also and they are much easier.

07-24-2010, 03:33 AM
that was very helpful, how can you pass a variable using sessions? i am aware that in order to have a seeion you need to include
at the top before anything else but then how can a variable from 1 page x-fer over to the next without the url?

07-24-2010, 04:33 AM
If I'm correct on this also, once you start the session and declare the variable like below it will stay in the session and you can call upon it on subsequent pages

$_SESSION['myusername'] = $myusername;[PHP]
Then you can use something like this on other pages to show a Welcome and myusername
if (isset($_SESSION['myusername'])){
Welcome, <?php echo $_SESSION['myusername']; ?>&nbsp;<a href='http://remotelystartedmn.com/logout.php'>Logout</a><br>

07-24-2010, 05:32 AM
so then you would have to declare the variable on top, after session_start();
using the tag?
$_SESSION['someVar'] = $someVar;

07-24-2010, 01:37 PM
Correct. After session start just use $_SESSION like a normal array but it will remain the same between page loads. Note that this can be confusing with clicking "back" etc.