Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 How to remove part of string?

View previous topic View next topic Go down 
AuthorMessage
MyLife
Member
Member
MyLife

Posts : 4
Join date : 2011-04-17

How to remove part of string? Empty
PostSubject: How to remove part of string?   How to remove part of string? EmptyThu Jun 23, 2011 7:15 am

Hi guys I Need some help, I coded a script in php to have active menu tabs when someone clicks on the links.

I accomplished this by comparing the href with the $_SERVER['REQUEST_URI'].
This worked great except when I login to my site it makes a random token and ads that to the url, this is only done ones but for that one time the menu will be displayed as not active.
So what I want to do is remove the token from $_SERVER['REQUEST_URI'] so my urls will always be correct.

Ive been thinking of something like str_replace("&id=", "" $_SERVER['REQUEST_URI']);

However I also need to remove the random generated token how should I remove that? the &id is always at the end of the url.
Back to top Go down
DreamsFade
Member
Member
DreamsFade

Posts : 12
Join date : 2011-04-17

How to remove part of string? Empty
PostSubject: Re: How to remove part of string?   How to remove part of string? EmptyThu Jun 23, 2011 7:15 am

Try this ...

<?php
$url = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

echo $url;
?>

It should only display the script name ... such as ...
if you enter it with, http.... index.php?id=1234&otherstuff=64564

It will display as "index.php"

(at least I think it will work).

================

But the real answer to your question is to use PHP SESSION.
When a log-in is successful, you set a session variable with the login script,
and when you return back to any other script, you can check for it.
There is nothing in the URL .. it's all done with SESSIONs.
Back to top Go down
MyLife
Member
Member
MyLife

Posts : 4
Join date : 2011-04-17

How to remove part of string? Empty
PostSubject: Re: How to remove part of string?   How to remove part of string? EmptyThu Jun 23, 2011 7:18 am

I tried your code but I forgot to tell I only need to remove one get variable.

Because some pages are like this:

mysite.com/index.php?page=1&s=2&sid=login token

Now what I need to have is something that will only remove &sid= + the random login token.

Ive been thinking about preg_replace.
But i don't know how to do it.
the sid is made by phpbb.

Now the only thing I need to do is use $_SERVER['REQUEST_URI'] to generate my site url and then I need to replace &sid + the random token with nothing.

Is this possible?
Back to top Go down
DreamsFade
Member
Member
DreamsFade

Posts : 12
Join date : 2011-04-17

How to remove part of string? Empty
PostSubject: Re: How to remove part of string?   How to remove part of string? EmptyThu Jun 23, 2011 7:18 am

With regex it's possible, but I don't know the sequence it needs.
I struggle with regex, like most people.

I'm still at a loss as to why you are not using PHP SESSION.
With your URL "get" method, what happens when they navigate pages?
I don't see how your login system is going to work.
Back to top Go down
MoonLight
Member
Member
MoonLight

Posts : 5
Join date : 2011-05-11

How to remove part of string? Empty
PostSubject: Re: How to remove part of string?   How to remove part of string? EmptyThu Jun 23, 2011 7:22 am

If I understand you correctly you should be able to do something like this:
Code:

$getValues =  $_GET; // optional, but leaves your $_GET array intact
unset($getValues['sid']);
$newURL = $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($getValues); 
Back to top Go down
Sponsored content




How to remove part of string? Empty
PostSubject: Re: How to remove part of string?   How to remove part of string? Empty

Back to top Go down
 

How to remove part of string?

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: How to remove part of string? Edit-trash Useless :: Trash-