Recent Topics

1 Sep 13, 2008 05:44    

My b2evolution Version: 2.4.x

I want to put in some PHP codes in a separate web page php file outside of the blog folder to check whether the blog user is logged in (based on their cookie) so I can display either "Login" or "View my blog" If user has not log in, I'll send them directly to the blog login url.

This page would appear on all part of the website. Sorry if this has been covered but I did search.

2 Sep 13, 2008 12:19

Hi Tommy,

In theory, you just have to include in your PHP file /blogs/inc/_main.inc.php in order to have access to b2evo functions. You then can use is_logged_in()

3 Sep 13, 2008 14:15

Hi tommyg3355,

You may try to read the cookie separate from any B2evo function.
But do try Austriaco's solution first. That one looks fine.
Connecting to B2evo requires these basic steps (from index.php)

/**
 * First thing: Do the minimal initializations required for b2evo:
 */
require_once dirname(__FILE__).'/conf/_config.php';

require_once $inc_path.'_main.inc.php';


You need to make the path to config.php though.

Please report if you succeeded in this.

Good luck

4 Sep 13, 2008 16:30

My blog install is in

http://redhotbrides.com/blog

I made a file call http://redhotbrides.com/new.php with the following:

<?php
require_once dirname(__FILE__).'/blog/conf/_config.php';

require_once $inc_path.'_main.inc.php';



if( is_logged_in())
 echo "<P>Logged in";
else
 echo "<p>Not logged in";

?>

It keeps telling me I'm not logged in. I think it also logs me out. I would login first then try this test.php page. When I go back to the blog pages, I am logged out.

*edit*
Wrapped code in [php][/php] tags

¥

5 Sep 13, 2008 16:50

Crack open /conf/_advanced.php and change this section of code :

/**
 * This is the path that will be associated to cookies.
 *
 * That means cookies set by this b2evo install won't be seen outside of this path on the domain below.
 *
 * @global string Default: preg_replace( '#https?://[^/]+#', '', $baseurl )
 */
$cookie_path = preg_replace( '#https?://[^/]+#', '', $baseurl );

To :

/**
 * This is the path that will be associated to cookies.
 *
 * That means cookies set by this b2evo install won't be seen outside of this path on the domain below.
 *
 * @global string Default: preg_replace( '#https?://[^/]+#', '', $baseurl )
 */
$cookie_path = '/';

You'll need to re-login to your admin afterwards

If that still fails then ( in the same file ) find :

/**
 * Short name of this system (will be used for cookies and notification emails).
 *
 * Change this only if you install mutliple b2evolutions on the same website.
 *
 * WARNING: don't play with this or you'll have tons of cookies sent away and your
 * readers surely will complain about it!
 *
 * You can change the notification email address alone a few lines below.
 *
 * @todo generate a random instance name at install and have it saved in the global params in the DB
 *
 * @global string Default: 'b2evo'
 */
$instance_name = 'evo'; // MUST BE A SINGLE WORD! NO SPACES!!

and change $instance_name to summat unique ;)

¥

6 Sep 13, 2008 16:52

b2evolution would set the login cookie with '/blog/' path, so any script outside '/blog/' will not see the cookie and therefore b2evolution will think you are logged out.

Two solutions: a) Tell b2evolution to set cookie's path as '/' (in conf/_advanced.php) or b) put your script inside '/blog/'

7 Sep 13, 2008 16:53

mwhahaha, to slow on the submit button :D

¥

8 Sep 13, 2008 17:10

But I gave him a second solution :)

9 Sep 13, 2008 17:23

Is that the sound of "desperately trying not to sound like I lost the race to the submit button" that I hear? :roll:

¥

10 Sep 13, 2008 19:12

Great stuff guys!

What I did was to make sure I wasn't going to break my blog software since its in production, I made 2 new files, _config2.php and _advance2.php

I put my test 'new.php' file outside the blog b/c that's where all my webpages are. I link to _config2.php in new.php

In _config2.php, it points to _advance2.php

Then in _advance2.php, i set as suggested:
$cookie_path = '/';

It works great. The last question I have is now my user is "Logged in", how can I get the url of where the user blog is located at?

I'm using UserBlog to allow all my users to make a blog. So when they're logged in from outside the blog, I want a link that can send them into their blog url.

EDIT:
I thought it was working fine .. but then I made some small changes just in the new.php to display something else and just poking around other files without changing anything, now I'm getting the errors shown below.

My test page is at http://redhotbrides.com/test.php:

Warning: Cannot modify header information - headers already sent by (output started at /home2/myhappi/public_html/redhotbrides.com/test.php:20) in /home2/myhappi/public_html/redhotbrides.com/blog/inc/sessions/model/_session.class.php on line 219

11 Sep 13, 2008 19:35

That's a fairly tough one as a user can be the owner of more than one blog.

If they're just the owner of a single blog then you could *probably* just query the DB for .... urmmm ... whatever the field is for blog owner ..... == $current_User->ID;

¥

12 Sep 13, 2008 20:03

Warning: Cannot modify header information - headers already sent by (output started at /home2/myhappi/public_html/redhotbrides.com/test.php:20) in /home2/myhappi/public_html/redhotbrides.com/blog/inc/sessions/model/_session.class.php on line 219

This error actually only happens in IE. In Firefox, it happened once but if I logout/login again its fine.

As for the blog url, i think its fine just to get them their first blog from the query result if they happen to have more then one, once in the blog app, they can navigate to the 2nd one then, how can I extract the url out specifically?

13 Sep 13, 2008 20:32

*ignores header errors for now*

<?php
$sql = 'select blog_ID from T_blogs where .... urm, the column for blog_owner_ID ... ='.$current_User->ID.' limit 1';
$foo = $DB->get_var( $sql );
$bar = get_Cache( 'BlogCache' );
$yay = $bar->get_by_ID( $foo );
echo '<a href="'.$yay->gen_blog_url().'" title="click me and hope like hell">Your blog</a>';
?>

( free typed ... so expect errors )

¥

14 Sep 13, 2008 22:45

$sql = 'select blog_ID from evo_blogs where blog_owner_user_ID ='.$current_User->ID.' limit 1';
$foo = $DB->get_var( $sql );
$bar = get_Cache( 'BlogCache' );
$yay = $bar->get_by_ID( $foo );
echo '<a href=\"'.$yay->gen_blog_url().'\" title="\click me and hope like hell\">Your blog</a>';

Fatal error: Call to undefined method Blog::gen_blog_url() in /home2/myhappi/public_html/redhotbrides.com/new.php on line 10

15 Sep 14, 2008 01:52

Replace gen_blog_url() by gen_blogurl()

I didn't check the rest of the code.

Good luck

16 Sep 14, 2008 04:29

Thanks!! So close to having it all work together.

gen_blogurl() works great.
The $current_User->ID doesn't return the blogID.

I'm still getting:
"Warning: Cannot modify header information - headers already sent by (output started at /home2/myhappi/public_html/redhotbrides.com/test.php:20) in /home2/myhappi/public_html/redhotbrides.com/blog/inc/sessions/model/_session.class.php on line 219 "

I'll summarize and post all my codes once I have it working, any further help much appreciated.

17 Sep 14, 2008 05:07

Can you provide an example of what $current_User->ID does generate?
Please add before the first line but after <?php:

echo '$current_User->ID = ' . $current_User->ID . "\n";

Never mind that headers already sent error. If it's really bothering you crack open /blog/inc/sessions/model/_session.class.php and place somewhere directly after the opening <?php:

// Basic error reporting (E_ALL without E_NOTICE) plus E_WARNING turned off
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);



*edit*

Probably you want to add this as first code line:

global $current_User;


but than again since it didn't throw an error probably not ;)

18 Sep 14, 2008 05:45

current_User->ID currently outputting nothing

Its throwing this error:
Notice: Trying to get property of non-object in /home2/myhappi/public_html/redhotbrides.com/new.php on line 6

However if I run this test file under the blog folder, it returns the ID# I need and everything works like a charm including no session error.

Maybe there's alternative ways to "include" my /blog/new.php file from outside the blog but i need to change the base path the script is run from b/c it doesn't work with a simple include statement I use b/c of the dirname path.

19 Sep 14, 2008 05:55

Ok starting to make sense now.
There is an error but you don't see it. Most likel\ you'll see the error in the source of the page where the 'Headers already sent' error shows.
The error you don't see is similar to 'Can't get property of non existent object' (forgot the actual line). This actual output causes - in it's turn - the 'Headers already sent' error.

Please do try with

global $current_User;


as first code line.
Do try with the

echo '$current_User->ID = ' . $current_User->ID . "\n";


as second lienesince it will tell you if it works.

If this doesn't work let's hope ¥åßßå has some ideas. I'm afraid you're deep into 'fingers crossed' territory as he mentioned ;)

Good luck

20 Sep 14, 2008 06:00

I tried the global. It didn't help.

This is working fine if the script is run under the /blog so right now as an alternative i'm looking into how to make another php file to execute this php in the blog and get the results back. That should work ok.

21 Sep 14, 2008 06:11

tommyg3355 wrote:

Maybe there's alternative ways to "include" my /blog/new.php file from outside the blog but i need to change the base path the script is run from b/c it doesn't work with a simple include statement I use b/c of the dirname path.

require_once $_SERVER['DOCUMENT_ROOT'] . '/blog/conf/_config.php'; 


Do check if you need the first slash before blog.

22 Sep 14, 2008 07:28

It looks like placing the script to check if a user is logged outside of the /blog application folder causes a lot of problems. Putting the script inside /blog and going with the early suggestions to check if a user is logged in and then to take action based on that is quite easy.

What I done to get around this was since the rest of my website was outside of the /blog folder. I made a link to a page inside the /blog folder which then calls the script to check if user is logged in.

You can see this in action at http://redhotbrides.com

There's a link "My Red Hot Brides Profile" on the left menu. When you click on that, it will bring you to a page inside the blog. Inside the blog, my left navigation menu now performs a check if the user is logged in or not, if they are, then it has a link to View their blog directly.

Here's the main code working:

<?php
require_once dirname(__FILE__).'/conf/_config2.php';

require_once $inc_path.'_main2.inc.php';


if( is_logged_in()) {
$sql = 'select blog_ID from  evo_blogs where blog_owner_user_ID ='.$current_User->ID.' limit 1';
$foo = $DB->get_var( $sql );
$bar = get_Cache( 'BlogCache' );
$yay = $bar->get_by_ID( $foo );

echo '<a href='.$yay->gen_blogurl().' title="\My red hot page\"><font color=yellow><b>Red Hot '.$current_User->login.'</font></b></a>';
}
else
  echo "<a href=\"http://redhotbrides.com/blog/htsrv/register.php?redirect_to=%2Fblog%2F%3Fblog%3D2\"><font color=white><b>Join Red Hot Brides</font></b></a>";

Would've definitely preferred being able to run these fuctions outside of the /blog but this works fine too. See it in action and refer some of your female friends to our site :)

Thanks so much everyone. Couldn't have done it without you guys.

*edit*
Wrapped code in [php][/php] tags

¥

23 Sep 14, 2008 09:40

¥åßßå wrote:

If that still fails then ( in the same file ) find :

/**
 * Short name of this system (will be used for cookies and notification emails).
 *
 * Change this only if you install mutliple b2evolutions on the same website.
 *
 * WARNING: don't play with this or you'll have tons of cookies sent away and your
 * readers surely will complain about it!
 *
 * You can change the notification email address alone a few lines below.
 *
 * @todo generate a random instance name at install and have it saved in the global params in the DB
 *
 * @global string Default: 'b2evo'
 */
$instance_name = 'evo'; // MUST BE A SINGLE WORD! NO SPACES!!

and change $instance_name to summat unique ;)

Did you try that ?

¥åßßå wrote:

*ignores header errors for now*

<?php
ob_start();
require_once dirname(__FILE__).'/blog/conf/_config2.php';
require_once $inc_path.'_main2.inc.php';
if( is_logged_in())
{
	$sql = 'select blog_ID from T_blogs where blog_owner_user_ID ='.$current_User->ID.' limit 1';
	$foo = $DB->get_var( $sql );
	$bar = get_Cache( 'BlogCache' );
	$yay = $bar->get_by_ID( $foo );
	echo '<a href="'.$yay->gen_blogurl().'" title="My red hot page" style="color:#ff0;font-weight:bold;">Red Hot '.$current_User->get_preferred_name().'</a>';
}
else
{
	user_register_link( '', '', 'Join Red Hot Brides' );
	user_login_link( '', '', 'Login to your account' );
}
$result = ob_get_clean();

echo $result;

?>

¥

24 Sep 14, 2008 10:02

I did not try that b/c I thought if I changed the "cookie" name, its like having 2 cookies ? B/c i just wanted to add a login link in my navigational menu throughout my website which would basically bring them to the /blog page.. The /blog is using the "evo" cookie so not sure how it would pick it up if a user is logged in with one cookie and I'm checking for another ..

25 Sep 14, 2008 10:07

Ok, what's happening is, you currently have a cookie ( evo ) that's set to /blog/ .. you need to change the instance name to force a *new* cookie that refers to / otherwise the evo cookie takes precedence and you'll never be logged in on /

¥


Form is loading...